3 ms·
It doesn’t — I was wrong. I was digging through its source code, and it turns out it isn’t memory-safe after all. Here’s the source code for stpcpy: https://gi
by winter_blue 7y ago
It doesn’t — I was wrong.
I was digging through its source code, and it turns out it isn’t memory-safe after all. Here’s the source code for stpcpy: https://github.com/ifduyue/musl/blob/master/src/string/stpcpy.c https://github.com/ifduyue/musl/blob/master/src/string/stpcp...
If dest is too small in the function linked to above, musl’s stpcpy will happy cause a buffer overflow.
Don’t know how or from where I got the impression that musl was a “safer” alternative to libc.
- cyphar 7y agoThe API of strcpy() is unsafe by design. The only way to make it safe is to not use it, and it's not musl's (nor glibc's) fault that this is the case. musl is interesting for a variety of other reasons, but being more memory safe isn't really one of them.
- _kst_ 7y agostrpy() is safe when used safely. That means that you've confirmed that the target is big enough. For example, there's no way this can fail: char s[10]; strcpy(s, "hello"); The problem is that it can also be used unsafely, and except in the simplest cases it's difficult or impossible to tell whether a give usage is safe. By contrast, gets() (which isn't even in the language anymore) is inherently unsafe, because you can't control what will appear on standard input.