3 ms·
I had no idea that's how strcpy is implemented. Buffer overflow exploits and the like aside (ah the good old days) that is really elegant!
by dnautics 13y ago
I had no idea that's how strcpy is implemented. Buffer overflow exploits and the like aside (ah the good old days) that is really elegant!
- huhtenberg 13y agoThis is not how strcpy is implemented. It is typically done in assembly.
- deleted 13y ago[deleted]
- andrewcooke 13y agohttps://sourceware.org/git/?p=glibc.git;a=blob;f=string/strcpy.c;h=b71f7536bca7fd2afc5854f681a3629cee2cab6f;hb=HEAD https://sourceware.org/git/?p=glibc.git;a=blob;f=string/strc...
- e12e 13y agoReally? I just did a "apt-get source libc6" and in strings/eglibc-2.17/string/strcpy.c I found: /* Copy SRC to DEST. */ char * strcpy (dest, src) char *dest; const char *src; { char c; char *__unbounded s = (char *__unbounded) CHECK_BOUNDS_LOW (src); const ptrdiff_t off = CHECK_BOUNDS_LOW (dest) - s - 1; size_t n; do { c = *s++; s[off] = c; } while (c != '\0'); n = s - src; (void) CHECK_BOUNDS_HIGH (src + n); (void) CHECK_BOUNDS_HIGH (dest + n); return dest; }
- peff 13y agoThat is the fallback implementation in case a platform does not have an optimized version available. Try `sysdeps/x86_64/strcpy.S` to see an optimized version.
- e12e 13y agoOooh, shiny. I'm not very familiar with how this is all tied together, thanks for the pointer. I was a bit thrown by the apparent lack of ifdefs and such in the c definition, but I see the asm-version starts out with: #ifndef USE_AS_STPCPY # define STRCPY strcpy #endif So I guess there is some logic set up in the configure-stage before building.
- troels 13y agoI assume the caller has the responsibility to ensure that x is actually large enough to contain y.
- tel 13y agoThey have to ensure that the strings are \0 terminated, so why not? C is definitely pre-condition-or-else.
- noir_lord 13y agoYou are probably right (and this is one of the reasons why as a language C and C++ can really blow up in your face).
- dnautics 13y agothat's why there are alternatives such as strncat.