Code:
char num[4];
strncpy(num, files[set]+ len - 16,3);
strcpy(cmd, "mkdir test/");
strcat(cmd, num);
system(cmd);
huh?
thanks
char num[4];
strncpy(num, files[set]+ len - 16,3);
strcpy(cmd, "mkdir test/");
strcat(cmd, num);
system(cmd);
open man:strncpy
results in a directory called 024_%FF in C++ instead of 024 in CCode:char num[4]; strncpy(num, files[set]+ len - 16,3); strcpy(cmd, "mkdir test/"); strcat(cmd, num); system(cmd);
huh?
thanks
Do _NOT_ use strncpy. _NOT EVER_. It is a highly dangerous. People use it to avoid buffer overflows with strcpy without thinking, but in those cases where strcpy would have overflowed a buffer, strncpy leaves a dangerous time bomb in the form of an unterminated string. As you just noticed.
results in a directory called 024_%FF in C++ instead of 024 in CCode:char num[4]; strncpy(num, files[set]+ len - 16,3); strcpy(cmd, "mkdir test/"); strcat(cmd, num); system(cmd);
huh?
thanks
Do _NOT_ use strncpy. _NOT EVER_.
Just declare the destination to be longer than the longest strncpy and bzero the destination before any strncpy.
Just declare the destination to be longer than the longest strncpy and bzero the destination before any strncpy.
Because life on the edge is the only life worth living. And because, you know, C is probably on more platforms and has more legacy libraries than any other language (I'm just guessing), and defines the defacto standard calling convention for libraries, and is relatively simple to learn (albeit with lots of gotchas for the unwary), and is reasonably efficient for low level code. And it isn't a niche language that almost nobody else uses; As wonderful as Cyclone may be, you're not going to get everyone to suddenly decide that that is the one language everybody needs to start using. Way too much competition for that spot. If you're asking why C got to be that way: it was pretty darn good for it's time, and still is actually.I don't know why C programmers are willing to put up with such an unsafe language. Why not try Cyclone instead (http://cyclone.thelanguage.org/)?
results in a directory called 024_%FF in C++ instead of 024 in CCode:char num[4]; strncpy(num, files[set]+ len - 16,3); strcpy(cmd, "mkdir test/"); strcat(cmd, num); system(cmd);
huh?
thanks
snprintf( cmd, sizeof(cmd), "mkdir test/%.*s", 3, &(files[set][len]) );
system( cmd );
strncpy(string1, string2, strlen(string2));
How is one time bomb worse than the other? With strcpy and buffer overflow, you potentially end up with changed variable values (from overwriting other memory). With strncpy and unterminated string, you could potentially go into some infinite loop if you try to do something with the string later.
I don't see how one can determine which has worse effects, but I'd be curious to hear your rationale.
void my_strcpy (char* dst, const char* src, size_t dest_size)
{
if (strlen (src) < dest_size) {
strcpy (dst, src);
} else if (dest_size > 0) {
memcpy (dst, src, dest_size - 1);
dst [dest_size - 1] = '\0';
assert (0 && "my_strcpy: src string too long");
} else {
assert (0 && "my_strcpy: dest_size == 0");
}
}
Done any serious software development recently?
When you have bugs in your software, you want bugs that are blatantly obvious so that you detect them in development, and don't wait until the customers detect them. strcpy trying to copy too much data has a much better chance of crashing your program in development, so you find the bug and fix it. strncpy limits the damage to the tiniest amount possible, so there is a much higher chance that the bug escapes from development to customers.
. . . other words snipped . . .
And a little side note, if you're using UTF-8 then both strncpy and my_strcpy are badly broken.
Thanks for your answer, although I don't care for the condescending tone of your rhetorical question.
Why..? AFAIK, UTF-8 promises not to insert any spurious 0 characters. Remember that strlen(some_utf8_string) still returns the number of chars in that string, not the number of characters.
"I'd be curious to hear your rationale" is what you posted; the reply was matching.