From 928b43eab613970ebe6448b294bba866d43d12bd Mon Sep 17 00:00:00 2001 From: Tor Andersson <tor.andersson@artifex.com> Date: Mon, 21 May 2018 11:09:17 +0200 Subject: Add a fz_strnlen function (strnlen is not standard C). --- include/mupdf/fitz/string-util.h | 6 ++++++ source/fitz/load-png.c | 2 +- source/fitz/string.c | 7 +++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/mupdf/fitz/string-util.h b/include/mupdf/fitz/string-util.h index fdf47c86..2bfc728f 100644 --- a/include/mupdf/fitz/string-util.h +++ b/include/mupdf/fitz/string-util.h @@ -10,6 +10,12 @@ Safe string functions */ +/* + fz_strnlen: Return strlen(s), if that is less than maxlen, or maxlen if + there is no null byte ('\0') among the first maxlen bytes. +*/ +size_t fz_strnlen(const char *s, size_t maxlen); + /* fz_strsep: Given a pointer to a C string (or a pointer to NULL) break it at the first occurrence of a delimiter char (from a given set). diff --git a/source/fitz/load-png.c b/source/fitz/load-png.c index 39c4ea81..cf1ab536 100644 --- a/source/fitz/load-png.c +++ b/source/fitz/load-png.c @@ -347,7 +347,7 @@ png_read_icc(fz_context *ctx, struct info *info, const unsigned char *p, unsigne fz_stream *mstm = NULL, *zstm = NULL; fz_colorspace *cs = NULL; size_t m = fz_mini(80, size); - size_t n = strnlen((const char *)p, m); + size_t n = fz_strnlen((const char *)p, m); if (n + 2 > m) { fz_warn(ctx, "invalid ICC profile name"); diff --git a/source/fitz/string.c b/source/fitz/string.c index dbcc2c22..a014ce89 100644 --- a/source/fitz/string.c +++ b/source/fitz/string.c @@ -14,6 +14,13 @@ fz_tolower(int c) return c; } +size_t +fz_strnlen(const char *s, size_t n) +{ + const char *p = memchr(s, 0, n); + return p ? p - s : n; +} + int fz_strcasecmp(const char *a, const char *b) { -- cgit v1.2.3