I have an array of 16 bytes that holds the name of an executable's segment.
char segname[16];
If the segment name length is less than 16 bytes, then the rest is padded with null bytes. Otherwise, there is no terminating null byte.
I want to compare segname
to various strings, e.g. __text
.
Is it legal to call strncmp
with a non-null-terminated string?
This post assumes it is legal. This source code makes it legal too. But my man's page says:
The
strncmp()
function lexicographically compares the null-terminated stringss1
ands2
.
The size passed to strncmp
will be the size of segname
.
I'm wondering what I should refer to.
According to the C99 standard, section 7.21.4.4, ยง3., it is legal:
Notice, however, that it says array of characters. By definition, if an array of characters is not null-terminated, it is not a string.