Dariusz isn’t sure what this line of code was meant to do. At his best guess, it was supposed to find an improperly terminated C string and add a null terminator character after it:

buf[strlen(buf)] = '\0';

Of course, if the string weren’t null terminated, we’ve entered nasal demon territory- the behavior of strlen is undefined for non-null terminated strings. If you present a C-compiler an undefined construct, it is allowed to do anything it likes, including make demons fly out of your nose.

A howitzer cannon for shooting yourself in the foot

But even on null terminated strings, this code is dangerous. Since arrays in C, like any sane language, are zero indexed, this code may attempt to access memory beyond the end of the array, overwriting whatever’s there with a null terminator.

This is the sort of code that happens when Java programmers attempt to write C, without understanding how they’ll shoot themselves in the foot

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!