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.
char
array which is not'\0'
-terminated is not a string! – Turnbullpossibly null-terminated array
next time. – Appendicectomychar
and always terminate the array. That allows to use other string functions. Better safe than sorry! – Turnbullnm
which parses binaries with mach-o format, and I don't have the choice,segname
is part of the section struct in the mach-o header files. I would have spend an extra char otherwise. By string we meannull-terminated byte arrays
even if there is no string type in C. Please check out the discussion below about the difference betweenread bytes
and compared bytes`. Man pages on my computers (osx / ubuntu) both talk about strings or null-terminated arrays, and as man pages differ I expected a citation from the standard. – Appendicectomystrncmp
. It is always good to read the standard before asking a question. Or - as I wrote - to have a correct man-page available. – Turnbull