Strlen_()

Restore its ability to reject a string longer than will fit within
size_t that was lost by moving away from strnlen().  Determine the
length inline rather than using strlen().

Move it from hacklib.c to alloc.c so that utility programs have easy
access, and remove the copy of it from dlb_main.c.

Fix a logic bug in str_start_is().  If a string was considered to
be too long, it exited the loop when n was 0 but also performed
post-decrement.  So after the loop, n would be -1 and the 'if (n==0)'
test would fail.  panic() would occur if the initial string matched
and happened to be LARGEST_INT-1 characters long.
This commit is contained in:
PatR
2024-01-25 12:16:39 -08:00
parent 47fa2eb000
commit 1c08982d56
4 changed files with 43 additions and 45 deletions
+5 -22
View File
@@ -1,4 +1,4 @@
/* NetHack 3.7 dlb_main.c $NHDT-Date: 1705957188 2024/01/22 20:59:48 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.25 $ */
/* NetHack 3.7 dlb_main.c $NHDT-Date: 1706213798 2024/01/25 20:16:38 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.27 $ */
/* Copyright (c) Kenneth Lorber, Bethesda, Maryland, 1993. */
/* NetHack may be freely redistributed. See license for details. */
@@ -18,8 +18,6 @@ ATTRNORETURN static void xexit(int) NORETURN;
ATTRNORETURN extern void panic(const char *, ...) NORETURN;
char *eos(char *); /* also used by dlb.c */
FILE *fopen_datafile(const char *, const char *);
unsigned FITSuint_(unsigned long long, const char *, int);
unsigned Strlen_(const char *, const char *, int);
#ifdef DLB
#ifdef DLBLIB
@@ -367,8 +365,7 @@ main(int argc UNUSED_if_no_DLB, char **argv UNUSED_if_no_DLB)
for (; ap < argc; ap++, nfiles++) {
if (nfiles == ldlimit)
grow_ld(&ld, &ldlimit, DLB_FILES_ALLOC / 5);
ld[nfiles].fname = (char *) alloc(Strlen(argv[ap]) + 1);
Strcpy(ld[nfiles].fname, argv[ap]);
ld[nfiles].fname = dupstr(argv[ap]);
}
}
@@ -385,8 +382,7 @@ main(int argc UNUSED_if_no_DLB, char **argv UNUSED_if_no_DLB)
if (nfiles == ldlimit)
grow_ld(&ld, &ldlimit, DLB_FILES_ALLOC / 5);
*(eos(buf) - 1) = '\0'; /* strip newline */
ld[nfiles].fname = (char *) alloc((int)strlen(buf) + 1);
Strcpy(ld[nfiles].fname, buf);
ld[nfiles].fname = dupstr(buf);
}
fclose(list);
}
@@ -409,7 +405,7 @@ main(int argc UNUSED_if_no_DLB, char **argv UNUSED_if_no_DLB)
ld[i].fsize = lseek(fd, 0, SEEK_END);
ld[i].foffset = flen;
slen += strlen(ld[i].fname); /* don't add null (yet) */
slen += (long) strlen(ld[i].fname); /* don't add null (yet) */
flen += ld[i].fsize;
close(fd);
}
@@ -425,7 +421,7 @@ main(int argc UNUSED_if_no_DLB, char **argv UNUSED_if_no_DLB)
/* caculate directory size */
dir_size = 40 /* header line (see below) */
+ ((nfiles + 1) * 11) /* handling+file offset+SP+newline */
+ slen + strlen(DLB_DIRECTORY); /* file names */
+ slen + (long) strlen(DLB_DIRECTORY); /* file names */
/* write directory */
write_dlb_directory(out, nfiles, ld, slen, dir_size, flen);
@@ -552,17 +548,4 @@ xexit(int retcd)
/*NOTREACHED*/
}
/* from hacklib.c */
unsigned
Strlen_(const char *str, const char *file, int line)
{
size_t len = strlen(str);
if (len >= LARGEST_INT) {
panic("%s:%d string too long", file, line);
/*NOTREACHED*/
}
return (unsigned) len;
}
/*dlb_main.c*/