mirror of
https://github.com/libexpat/libexpat.git
synced 2025-04-06 13:45:00 +00:00
Merge pull request #1000 from bluhm/strndup-nul
tests: `portable_strndup` must not read source string beyond NUL byte
This commit is contained in:
commit
0a27875087
2 changed files with 15 additions and 1 deletions
|
@ -40,6 +40,11 @@
|
|||
Release 2.7.2 ??? ????? ?? ????
|
||||
Other changes:
|
||||
#994 docs: Drop AppVeyor badge
|
||||
#1000 tests: Fix portable_strndup
|
||||
|
||||
Special thanks to:
|
||||
Alexander Bluhm
|
||||
Theo Buehler
|
||||
|
||||
Release 2.7.1 Thu March 27 2025
|
||||
Bug fixes:
|
||||
|
|
|
@ -303,7 +303,14 @@ duff_reallocator(void *ptr, size_t size) {
|
|||
return realloc(ptr, size);
|
||||
}
|
||||
|
||||
// Portable remake of strndup(3) for C99; does not care about space efficiency
|
||||
// Portable remake of strnlen(3) for C99
|
||||
static size_t
|
||||
portable_strnlen(const char *s, size_t maxlen) {
|
||||
const char *const end = (const char *)memchr(s, '\0', maxlen);
|
||||
return (end == NULL) ? maxlen : (size_t)(end - s);
|
||||
}
|
||||
|
||||
// Portable remake of strndup(3) for C99
|
||||
char *
|
||||
portable_strndup(const char *s, size_t n) {
|
||||
if ((s == NULL) || (n == SIZE_MAX)) {
|
||||
|
@ -311,6 +318,8 @@ portable_strndup(const char *s, size_t n) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
n = portable_strnlen(s, n);
|
||||
|
||||
char *const buffer = (char *)malloc(n + 1);
|
||||
if (buffer == NULL) {
|
||||
errno = ENOMEM;
|
||||
|
|
Loading…
Add table
Reference in a new issue