Fix bug in strndup implementation

The strlen() could go past the n bytes and into a memory address we
don't have read access to.
This commit is contained in:
Adrian Johnson 2023-01-03 15:08:48 +10:30
parent 5e0e40e3c5
commit 82aa3fb80e

View file

@ -37,15 +37,19 @@ char *
strndup (const char *s,
size_t n)
{
const char *end;
size_t len;
char *sdup;
if (s == NULL)
return NULL;
len = strlen (s);
if (len > n)
end = memchr (s, 0, n);
if (end)
len = end - s;
else
len = n;
sdup = (char *) _cairo_malloc (len + 1);
if (sdup != NULL) {
memcpy (sdup, s, len);