sds icon indicating copy to clipboard operation
sds copied to clipboard

Fixed sdscat*() bug: read memory error

Open iiol opened this issue 5 years ago • 5 comments

Example:

sds str;

str = sdsnew("Hi ");
str = sdscat(str, str);
printf("%s\n", str); // not "Hi Hi "

iiol avatar Jan 04 '20 02:01 iiol

You can simplify this with memmove.

UmanShahzad avatar May 28 '20 15:05 UmanShahzad

Wouldn't a call to memmove affect performance for big strings ? Is it not better to mention in the doc that sdscat should not take two same pointers ?

cassepipe avatar Jan 16 '21 17:01 cassepipe

It depends; some implementations of memmove will check for an overlap first, and if there isn't one will delegate to memcpy.

Example of musl libc doing this: https://git.musl-libc.org/cgit/musl/tree/src/string/memmove.c#n15

UmanShahzad avatar Jan 16 '21 18:01 UmanShahzad

This is a non-issue. This is not the intended way of using sdscat function. It's defined with this prototype: sds sdscatlen(sds s, const void *t, size_t len) which promises not to modify the second argument t. It forbids the case where s == t. Maybe, add a comment about this limitation

fractalb avatar Mar 24 '21 11:03 fractalb

Then how is the overlap case ever going to be supported?

Either brand new interfaces with the exact same code but with memcpy -> memmove (and there are many functions calling into this), thus significantly complicating the interface, or remove const and break backwards compatibility at the interface (warnings will be generated for users who were passing in const arguments into t).

The simplest solution is to just use memmove, which has practically no extra cost because it will still delegate to memcpy, and note in the comments this edge case.

UmanShahzad avatar Mar 24 '21 12:03 UmanShahzad