Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed sdscat*() bug: read memory error #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

iiol
Copy link

@iiol iiol commented Jan 4, 2020

Example:

sds str;

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

@UmanShahzad
Copy link

You can simplify this with memmove.

@cassepipe
Copy link

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 ?

@UmanShahzad
Copy link

UmanShahzad commented Jan 16, 2021

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

@fractalb
Copy link

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

@UmanShahzad
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants