45

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 strings s1 and s2.

The size passed to strncmp will be the size of segname.

I'm wondering what I should refer to.

6
  • 19
    An char array which is not '\0'-terminated is not a string! Jan 1, 2017 at 20:32
  • 9
    @Olaf Right, I will call it possibly null-terminated array next time.
    – Bilow
    Jan 1, 2017 at 20:43
  • You missed my point! There is no string type in C. However, you should consider spending that extra char and always terminate the array. That allows to use other string functions. Better safe than sorry! Jan 1, 2017 at 22:53
  • 4
    Not sure which man-page you cite. But the POSIX man-page on Ubuntu says "The strncmp() function shall compare not more than n bytes (bytes that follow a null byte are not compared) from the array pointed to by s1 to the array pointed to by s2. " which should be clear and is directly from the C standard. Maybe you want to update your man-pages? Jan 1, 2017 at 22:58
  • 1
    It is not me having a problem with strncmp. It is always good to read the standard before asking a question. Or - as I wrote - to have a correct man-page available. Jan 2, 2017 at 12:12

2 Answers 2

67

According to the C99 standard, section 7.21.4.4, §3., it is legal:

The strncmp function returns an integer greater than, equal to, or less than zero, accordingly as the possibly null-terminated array pointed to by s1 is greater than, equal to, or less than the possibly null-terminated array pointed to by s2.

Notice, however, that it says array of characters. By definition, if an array of characters is not null-terminated, it is not a string.

12
  • 10
    C99 has been superseded; C2011 is the current C standard. Its specification for strncmp() in section 7.24.4.4, does indeed omit any requirement that the arrays to be compared be null-terminated, however. Jan 1, 2017 at 20:59
  • 9
    It should be observed, however, that the omission of a requirement for null termination does not allow access outside the bounds of either input array. It is incumbent on the caller to ensure that the third argument is appropriate to avoid the function overrunning the bounds of either array. The caller risks invoking undefined behavior if he fails to do so. Jan 1, 2017 at 21:02
  • 1
    @JohnBollinger: When the Standard indicates that characters followiing a zero byte are not compared, should that to be taken to imply that an implementation will behave as though it does not read such characters? It would hardly seem unusual that an application might need to compare a zero-padded string [which might lack a trailing zero if it precisely fills its buffer] with a zero-terminated one [whose buffer might be shorter than that of the zero-padded string], and such a guarantee would be needed to make strncmp suitable for such purpose.
    – supercat
    Jan 1, 2017 at 22:45
  • 3
    In the beginning of the section that explains <string.h>, the standard says that the argument n specifies the length of the array. strncmp also doesn't required NULs. This implies that nothing before s1 or s2 and nothing at or beyond s1+n or s2+n will be accessed.
    – giusti
    Jan 1, 2017 at 23:21
  • 2
    Come to think of it, in the worst case the implementation has to access all characters of s1 in the comparison. It doesn't compare characters at different lags from their pointers. I'm tempted to say that if s1 is null terminated and smaller than s2, then s2 will only be accessed at its first strlen(s1) characters. But I'm unsure.
    – giusti
    Jan 1, 2017 at 23:44
15

The strncmp function compares not more than n characters (characters that follow a null character are not compared) from the array pointed to by s1 to the array pointed to by s2.

Specification 7.24.4.2 says that.C11 standard.

Characters that don't follow a null charcaters are not compared so it expects null ended character array or string.1

You can use non-null terminated characters too in here but in that case we have to specify the length upto which we have to check it which is useful in some cases.

Corrections


[1] That characters that don't follow a null character are not compared does not mean that strncmp expects null-terminated strings. It just means that strncmp needs a special case so as to say (for example) that abc\0def... and abc\0xyz... compare equal. There's nothing wrong with comparing two char arrays that aren't null-terminated (up to the specified length) or comparing one null terminated char array with another that isn't null terminated
This is directly added from the comment of David Hammen

10
  • 4
    Note "... follow a NULL charcaters...." makes more sense as "... follow a null character ... ". NULL is the null pointer constant associated with pointers. Save the uppercase NULL for discussion about pointers. Jan 1, 2017 at 21:08
  • Or you can write NUL, as it is usually denoted in ASCII and ASCII-derived standards.
    – giusti
    Jan 1, 2017 at 21:10
  • @giusti C is not specified to use ASCII, yet commonly does. That is why NUL (an ASCII defined value) is not used in the C specification (except in a non-normative footnote.) A good alternative to the null character is '\0'. See this Jan 1, 2017 at 21:13
  • 2
    "Characters that don't follow a null charcaters are not compared so it expects null ended character array or string": that answer is incorrect. The text you quoted establishes a restriction for strncmp: it will never read past a null character, if one is present. It does not impose a requirement that there must be a null character. Also with strncmp you always have to specify the maximum number of characters that should be checked.
    – giusti
    Jan 2, 2017 at 12:42
  • 1
    Regarding an edit: Fix this: Characters that don't follow a null charcaters are not compared so it expects null ended character array or string. That characters that don't follow a null character are not compared does not mean that strncmp expects null-terminated strings. It just means that strncmp needs a special case so as to say (for example) that abc\0def... and abc\0xyz... compare equal. There's nothing wrong with comparing two char arrays that aren't null-terminated (up to the specified length) or comparing one null terminated char array with another that isn't null terminated. Jan 2, 2017 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.