For performance reasons, implementations of the standard string functions will often process the data in naturally aligned register-width chunks. This can cause read access past the end of the source data objects, but the alignment guarantees that the code behaves exactly like a naive implementation with respect to memory exceptions. Each wide access is contained within a single page, and no pages are touched that would not also be touched by a byte-wise implementation.
I would claim that such implementations are covered by C's as-if rule, that is, they behave the same "as if" they were following the abstract functional specifications.
An example of such an optimized implementation would be OpenSolaris's strcmp()
for SPARC v8. This is code I wrote some fifteen years ago, along with other performance-optimized string functions.
Various memory checker tools will complain about such code, however, because its use can lead to access beyond the limits of the allocated data object, even though the out-of-bounds read access is harmless by design.
strncmp()
. – Novellastrncmp()
will only need to compare a singlechar
from each of the strings in your example. I second the false positive. – Eadith"test string"
is not a string but a string literal. A string literal may have multiple null characters. A string has only one. IAC, this string literal certainly has at least one null character and does not pose any problem here. – Niggerstrcmp
, which were caused exactly by the problem you mentioned. – Paroicous