mirror of
https://github.com/RubyMetric/chsrc
synced 2025-06-08 03:04:03 +08:00
Fix bug of strcpy to wrong place during realloc
This commit is contained in:
parent
1d34248a1e
commit
251ea5c827
5
Makefile
5
Makefile
@ -1,6 +1,7 @@
|
|||||||
CFLAGS =
|
CFLAGS =
|
||||||
|
|
||||||
TARGET = chsrc
|
TARGET = chsrc
|
||||||
|
TEST_TARGET = test_$(TARGET)
|
||||||
#=======================
|
#=======================
|
||||||
|
|
||||||
all:
|
all:
|
||||||
@ -8,8 +9,8 @@ all:
|
|||||||
@gcc chsrc.c $(CFLAGS) -o $(TARGET)
|
@gcc chsrc.c $(CFLAGS) -o $(TARGET)
|
||||||
|
|
||||||
test:
|
test:
|
||||||
@gcc test_helper.c -o test
|
@gcc test_helper.c -o $(TEST_TARGET)
|
||||||
@./test
|
@./$(TEST_TARGET)
|
||||||
|
|
||||||
test_cmd: $(TARGET)
|
test_cmd: $(TARGET)
|
||||||
./$(TARGET) list mirror
|
./$(TARGET) list mirror
|
||||||
|
18
helper.h
18
helper.h
@ -176,7 +176,7 @@ xy_2strjoin (const char* str1, const char* str2)
|
|||||||
static char*
|
static char*
|
||||||
xy_strjoin (unsigned int count, ...)
|
xy_strjoin (unsigned int count, ...)
|
||||||
{
|
{
|
||||||
size_t al_fixed = 256;
|
size_t al_fixed = 128;
|
||||||
char* ret = calloc(1, al_fixed);
|
char* ret = calloc(1, al_fixed);
|
||||||
// 已分配次数
|
// 已分配次数
|
||||||
int al_times = 1;
|
int al_times = 1;
|
||||||
@ -194,14 +194,26 @@ xy_strjoin (unsigned int count, ...)
|
|||||||
|
|
||||||
for(int i=0; i<count; i++)
|
for(int i=0; i<count; i++)
|
||||||
{
|
{
|
||||||
|
// 是否需要重新分配
|
||||||
|
bool need_realloc = false;
|
||||||
|
|
||||||
str = va_arg(args, const char*);
|
str = va_arg(args, const char*);
|
||||||
al_need += strlen(str);
|
al_need += strlen(str);
|
||||||
if (al_need > al_cur) {
|
while (al_need > al_cur) {
|
||||||
al_times += 1; al_cur = al_times * al_fixed;
|
al_times += 1; al_cur = al_times * al_fixed;
|
||||||
|
need_realloc = true;
|
||||||
|
}
|
||||||
|
// printf("al_times %d, al_need %zd, al_cur %zd\n", al_times, al_need, al_cur);
|
||||||
|
if (need_realloc) {
|
||||||
|
ptrdiff_t diff = cur - ret;
|
||||||
ret = realloc(ret, al_cur);
|
ret = realloc(ret, al_cur);
|
||||||
if (NULL==ret) { xy_error ("xy: No availble memory!"); return NULL; }
|
cur = ret + diff;
|
||||||
|
}
|
||||||
|
if (NULL==ret) {
|
||||||
|
xy_error ("xy: No availble memory!"); return NULL;
|
||||||
}
|
}
|
||||||
strcpy(cur, str);
|
strcpy(cur, str);
|
||||||
|
// puts(ret);
|
||||||
cur += strlen(str);
|
cur += strlen(str);
|
||||||
}
|
}
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
* File : test_helper.c
|
* File : test_helper.c
|
||||||
* Authors : Aoran Zeng <ccmywish@qq.com>
|
* Authors : Aoran Zeng <ccmywish@qq.com>
|
||||||
* Created on : <2023-08-30>
|
* Created on : <2023-08-30>
|
||||||
* Last modified : <2023-08-30>
|
* Last modified : <2023-09-02>
|
||||||
*
|
*
|
||||||
* test_helper:
|
* test_helper:
|
||||||
*
|
*
|
||||||
@ -18,8 +18,9 @@ main (int argc, char const *argv[])
|
|||||||
xy_useutf8();
|
xy_useutf8();
|
||||||
puts(xy_2strjoin("Xi", "'an"));
|
puts(xy_2strjoin("Xi", "'an"));
|
||||||
puts(xy_strjoin (2, "Xi", "'an"));
|
puts(xy_strjoin (2, "Xi", "'an"));
|
||||||
puts(xy_strjoin(3, "中文输出", " XiangYang", " shan shui"));
|
puts(xy_strjoin(3, "屈身守分,", "以待天时,", "不可与命争也"));
|
||||||
puts(xy_strjoin(4, "中文输出2", " XianYan", " hao", " feng jing"));
|
puts(xy_strjoin(4, "水落鱼梁浅,", "天寒梦泽深。", "羊公碑字在,", "读罢泪沾襟。"));
|
||||||
|
puts(xy_strjoin(6, "楚山横地出,", "汉水接天回。", "冠盖非新里,", "章华即旧台。", "习池风景异,", "归路满尘埃。"));
|
||||||
|
|
||||||
xy_success("成功:输出成功内容");
|
xy_success("成功:输出成功内容");
|
||||||
xy_info("信息: 输出信息内容");
|
xy_info("信息: 输出信息内容");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user