Benjamin Schieder

[TECHSUCKS] STRCPY AND \0

2009 August 31

Suppose you have a char foo[11]; that you want to put a 10-Byte Text into. For simplicity you copy and paste a line that you had used just a little earlier:

strncpy(foo, "1234567890", 10);

To me, this first looked harmless and even worked fine. At least, mostly. I pretty soon found out that strncpy doesnr't add a final \\0. So in the end it became this after all:
sprintf(foo, "1234567890");


EOF

Category: blog

Tags: TechSucks