From 7c1512fa99dd5222a93b9109573f219714e1f892 Mon Sep 17 00:00:00 2001 From: PatR Date: Mon, 22 Jan 2024 12:59:51 -0800 Subject: [PATCH] avoid strnlen() Replace the single strnlen() use with strlen() so as not to require something from posix.1 (2008) when we supposedly only require c99. If someone manages to produce a string that's longer than will fit within size_t, strlen()'s length count will wrap and Strlen_() might not notice. Since size_t has to be at least 32 bits, that doesn't seem like something to worry about. If checking for size_t overflow is considered essential, we should just switch to counting the length via an in-line loop that imposes a similar limit to strnlen(). --- src/hacklib.c | 17 ++++++++++++----- util/dlb_main.c | 7 ++++--- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/hacklib.c b/src/hacklib.c index 32b2cc590..6a642e13b 100644 --- a/src/hacklib.c +++ b/src/hacklib.c @@ -1,4 +1,4 @@ -/* NetHack 3.7 hacklib.c $NHDT-Date: 1596498172 2020/08/03 23:42:52 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.85 $ */ +/* NetHack 3.7 hacklib.c $NHDT-Date: 1705957184 2024/01/22 20:59:44 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.115 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /*-Copyright (c) Michael Allison, 2007. */ /* Copyright (c) Robert Patrick Rankin, 1991 */ @@ -235,10 +235,14 @@ c_eos(const char *s) /* like strlen(3) but returns unsigned and panics if string is unreasonably long */ unsigned -Strlen_(const char *str, const char *file, int line){ - size_t len = strnlen(str, LARGEST_INT); +Strlen_( + const char *str, + const char *file, + int line) +{ + size_t len = strlen(str); - if (len == LARGEST_INT) + if (len >= LARGEST_INT) panic("%s:%d string too long", file, line); return (unsigned) len; } @@ -246,7 +250,10 @@ Strlen_(const char *str, const char *file, int line){ /* determine whether 'str' starts with 'chkstr', possibly ignoring case; * panics on huge strings */ boolean -str_start_is(const char *str, const char *chkstr, boolean caseblind) +str_start_is( + const char *str, + const char *chkstr, + boolean caseblind) { int n = LARGEST_INT; diff --git a/util/dlb_main.c b/util/dlb_main.c index 1d8523057..b49826276 100644 --- a/util/dlb_main.c +++ b/util/dlb_main.c @@ -1,4 +1,4 @@ -/* NetHack 3.7 dlb_main.c $NHDT-Date: 1687547434 2023/06/23 19:10:34 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.24 $ */ +/* NetHack 3.7 dlb_main.c $NHDT-Date: 1705957188 2024/01/22 20:59:48 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.25 $ */ /* Copyright (c) Kenneth Lorber, Bethesda, Maryland, 1993. */ /* NetHack may be freely redistributed. See license for details. */ @@ -556,12 +556,13 @@ xexit(int retcd) unsigned Strlen_(const char *str, const char *file, int line) { - size_t len = strnlen(str, LARGEST_INT); + size_t len = strlen(str, LARGEST_INT); - if (len == LARGEST_INT) { + if (len >= LARGEST_INT) { panic("%s:%d string too long", file, line); /*NOTREACHED*/ } return (unsigned) len; } + /*dlb_main.c*/