cloudwu / cstring Goto Github PK
View Code? Open in Web Editor NEWA simple C string lib
License: MIT License
A simple C string lib
License: MIT License
=================================================================
==24927==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 1058 byte(s) in 1 object(s) allocated from:
#0 0x4b9808 (/home/ubuntu/fuzz/cstring/app+0x4b9808)
#1 0x4f13a0 (/home/ubuntu/fuzz/cstring/app+0x4f13a0)
SUMMARY: AddressSanitizer: 1058 byte(s) leaked in 1 allocation(s).
我用您写的这个库,cstring_cat连接了一个很长的字符串,最后用char* str = CSTRING(cstr_buf)->cstr;获得了一个char* ,但是strlen(str)的时候,程序就崩溃了,大概几千个字符的时候没任何问题,但是上万了就出问题了?ps:我是新手。
能解答下吗?谢谢!
云风老师您好,我在仿造你的程序写自己的字符串类的时候,发现生成的字符串有错误,仔细分析原因,认为问题应该出在vsnprintf这个函数的调用处,根据manpage
The functions vprintf(), vfprintf(), vdprintf(), vsprintf(), vsnprintf() are equivalent to the functions printf(), fprintf(), dprintf(), sprintf(), snprintf(), respectively, except that they are called with a va_list instead of a variable number of arguments. These functions do not call the va_end macro. Because they invoke the va_arg macro, the value of ap is undefined after the call. See stdarg(3).
vsnprintf会修改传入的ap,所以如果程序在第一次调用vsnprintf发现buffer空间不够长之后,再次调用vsnprintf,就会出现未定义行为。
您提供的代码在我的64位linux机器上无法编译,把Makefile中的-march=i686改为-m32之后,测试没有发现问题,但是删掉-m32之后测试也会出现错误。
cstring库中使用si->total * 5 >= si->size * 4
的检测结果来对string_interning的hash表进行扩容,但是我发现total字段从未更新过。我使用如下代码进行测试:
// 在test.c的main方法中
char str[2];
str[1] = '\0';
for (int i = 32; i < 127; i++) {
str[0] = (char) i;
cstring_persist(str, 1);
}
// 在cstring.c的129行下面
printf("total: %d, index: %d, size: %d\n", S.total, S.index, S.size);
输出(仅取了最后一行):
total: 0, index: 95, size: 16
测试结果显示total没有更新过,hash表也没有相应扩容,反而是index与interning的短字符串一致。因而我认为可以去掉total字段,用index的值取代之来进行判断。
如果把hash_blob
函数的实现改成直接return 1;
,然后执行下面两行代码:
CSTRING_LITERAL(foostr0, "bye world");
CSTRING_LITERAL(foostr1, "hello world");
在执行完第一行代码后S.hash[1]
里存的是"bye world",然而在执行完第二行代码后S.hash[1]
变成了“hello world"。
在函数interning
末尾的这行代码si->hash[index] = n;
之前,index
并没有更新。
As I see, there are two ways of using "__sync_synchronize" thi the cstring project.
First, used at the beginning of grab/release api function;
Second, used after calling cstring_clone.
I can't figure out why cstring need this memory barrier here.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.