summaryrefslogtreecommitdiffstats
path: root/src/util
diff options
context:
space:
mode:
authorSamuel Iglesias Gonsalvez <siglesias@igalia.com>2015-09-29 16:10:02 +0200
committerSamuel Iglesias Gonsalvez <siglesias@igalia.com>2015-09-30 08:13:07 +0200
commitf3afcbecc63ec565a0386cda554d145ca908367d (patch)
tree35071a2c95ba2c99e134b949fb870fa3bc367176 /src/util
parent023165a734b3bae52a449ad01bc1ea5ba4384ec1 (diff)
downloadexternal_mesa3d-f3afcbecc63ec565a0386cda554d145ca908367d.zip
external_mesa3d-f3afcbecc63ec565a0386cda554d145ca908367d.tar.gz
external_mesa3d-f3afcbecc63ec565a0386cda554d145ca908367d.tar.bz2
util: use strnlen() in strndup() implementations
If the string being copied is not NULL-terminated the result of strlen() is undefined. Signed-off-by: Samuel Iglesias Gonsalvez <siglesias@igalia.com> Reviewed-by: Neil Roberts <neil@linux.intel.com> Reviewed-by: Jose Fonseca <jfonseca@vmware.com>
Diffstat (limited to 'src/util')
-rw-r--r--src/util/ralloc.c5
-rw-r--r--src/util/strndup.c5
2 files changed, 2 insertions, 8 deletions
diff --git a/src/util/ralloc.c b/src/util/ralloc.c
index 01719c8..e07fce7 100644
--- a/src/util/ralloc.c
+++ b/src/util/ralloc.c
@@ -359,10 +359,7 @@ ralloc_strndup(const void *ctx, const char *str, size_t max)
if (unlikely(str == NULL))
return NULL;
- n = strlen(str);
- if (n > max)
- n = max;
-
+ n = strnlen(str, max);
ptr = ralloc_array(ctx, char, n + 1);
memcpy(ptr, str, n);
ptr[n] = '\0';
diff --git a/src/util/strndup.c b/src/util/strndup.c
index ca1c6f5..5ceb32f 100644
--- a/src/util/strndup.c
+++ b/src/util/strndup.c
@@ -35,10 +35,7 @@ strndup(const char *str, size_t max)
if (!str)
return NULL;
- n = strlen(str);
- if (n > max)
- n = max;
-
+ n = strnlen(str, max);
ptr = (char *) calloc(n + 1, sizeof(char));
if (!ptr)
return NULL;