make use of memchr() in strnlen() replacement after all
turns out the comment advising against it was bogus - unlike for memcmp(), the standard does indeed prescribe that the memchr() implementation may not read past the first occurrence of the searched char.
This commit is contained in:
		
							parent
							
								
									357dd51405
								
							
						
					
					
						commit
						3eea668052
					
				
					 1 changed files with 2 additions and 5 deletions
				
			
		|  | @ -225,11 +225,8 @@ memrchr( const void *s, int c, size_t n ) | |||
| size_t | ||||
| strnlen( const char *str, size_t maxlen ) | ||||
| { | ||||
| 	size_t len; | ||||
| 
 | ||||
| 	/* It's tempting to use memchr(), but it's allowed to read past the end of the actual string. */ | ||||
| 	for (len = 0; len < maxlen && str[len]; len++) {} | ||||
| 	return len; | ||||
| 	const char *estr = memchr( str, 0, maxlen ); | ||||
| 	return estr ? (size_t)(estr - str) : maxlen; | ||||
| } | ||||
| 
 | ||||
| #endif | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue