Module Name: src Committed By: christos Date: Tue Jan 23 15:32:54 UTC 2024
Modified Files: src/lib/libc/regex: engine.c regsub.c src/lib/libc/stdio: open_memstream.c open_wmemstream.c Log Message: handle sizeof(off_t) > sizeof(size_t) To generate a diff of this commit: cvs rdiff -u -r1.29 -r1.30 src/lib/libc/regex/engine.c cvs rdiff -u -r1.3 -r1.4 src/lib/libc/regex/regsub.c cvs rdiff -u -r1.1 -r1.2 src/lib/libc/stdio/open_memstream.c \ src/lib/libc/stdio/open_wmemstream.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/lib/libc/regex/engine.c diff -u src/lib/libc/regex/engine.c:1.29 src/lib/libc/regex/engine.c:1.30 --- src/lib/libc/regex/engine.c:1.29 Thu Feb 25 16:47:46 2021 +++ src/lib/libc/regex/engine.c Tue Jan 23 10:32:54 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: engine.c,v 1.29 2021/02/25 21:47:46 christos Exp $ */ +/* $NetBSD: engine.c,v 1.30 2024/01/23 15:32:54 christos Exp $ */ /*- * SPDX-License-Identifier: BSD-3-Clause @@ -41,7 +41,7 @@ #ifdef __FBSDID __FBSDID("$FreeBSD: head/lib/libc/regex/engine.c 368358 2020-12-05 03:16:05Z kevans $"); #endif -__RCSID("$NetBSD: engine.c,v 1.29 2021/02/25 21:47:46 christos Exp $"); +__RCSID("$NetBSD: engine.c,v 1.30 2024/01/23 15:32:54 christos Exp $"); #include <stdbool.h> @@ -784,13 +784,13 @@ backref( if (m->pmatch[i].rm_eo == -1) return(NULL); assert(m->pmatch[i].rm_so != -1); - len = m->pmatch[i].rm_eo - m->pmatch[i].rm_so; + len = (size_t)(m->pmatch[i].rm_eo - m->pmatch[i].rm_so); if (len == 0 && rec++ > MAX_RECURSION) return(NULL); assert(stop - m->beginp >= len); if (sp > stop - len) return(NULL); /* not enough left to match */ - ssp = m->offp + m->pmatch[i].rm_so; + ssp = m->offp + (size_t)m->pmatch[i].rm_so; if (memcmp(sp, ssp, len) != 0) return(NULL); while (m->g->strip[ss] != SOP(O_BACK, i)) Index: src/lib/libc/regex/regsub.c diff -u src/lib/libc/regex/regsub.c:1.3 src/lib/libc/regex/regsub.c:1.4 --- src/lib/libc/regex/regsub.c:1.3 Mon Feb 29 17:10:13 2016 +++ src/lib/libc/regex/regsub.c Tue Jan 23 10:32:54 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: regsub.c,v 1.3 2016/02/29 22:10:13 aymeric Exp $ */ +/* $NetBSD: regsub.c,v 1.4 2024/01/23 15:32:54 christos Exp $ */ /*- * Copyright (c) 2015 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include <sys/cdefs.h> #if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: regsub.c,v 1.3 2016/02/29 22:10:13 aymeric Exp $"); +__RCSID("$NetBSD: regsub.c,v 1.4 2024/01/23 15:32:54 christos Exp $"); #endif #include <sys/param.h> @@ -134,7 +134,7 @@ regsub1(char **buf, size_t len, const ch addchar(&s, c); } else if (rm[i].rm_so != -1 && rm[i].rm_eo != -1) { size_t l = (size_t)(rm[i].rm_eo - rm[i].rm_so); - addnstr(&s, str + rm[i].rm_so, l); + addnstr(&s, str + (size_t)rm[i].rm_so, l); } } Index: src/lib/libc/stdio/open_memstream.c diff -u src/lib/libc/stdio/open_memstream.c:1.1 src/lib/libc/stdio/open_memstream.c:1.2 --- src/lib/libc/stdio/open_memstream.c:1.1 Sun Oct 12 20:40:36 2014 +++ src/lib/libc/stdio/open_memstream.c Tue Jan 23 10:32:54 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: open_memstream.c,v 1.1 2014/10/13 00:40:36 christos Exp $ */ +/* $NetBSD: open_memstream.c,v 1.2 2024/01/23 15:32:54 christos Exp $ */ /*- * Copyright (c) 2013 Advanced Computing Technologies LLC @@ -31,7 +31,7 @@ #if 0 __FBSDID("$FreeBSD: head/lib/libc/stdio/open_memstream.c 247411 2013-02-27 19:50:46Z jhb $"); #endif -__RCSID("$NetBSD: open_memstream.c,v 1.1 2014/10/13 00:40:36 christos Exp $"); +__RCSID("$NetBSD: open_memstream.c,v 1.2 2024/01/23 15:32:54 christos Exp $"); #include "namespace.h" #include <assert.h> @@ -51,16 +51,21 @@ struct memstream { size_t offset; }; +static __inline size_t +off_t_to_size_t(off_t off) +{ + if (off < 0 || off >= SSIZE_MAX) + return SSIZE_MAX - 1; + return (size_t)off; +} + static int memstream_grow(struct memstream *ms, off_t newoff) { char *buf; size_t newsize; - if (newoff < 0 || newoff >= SSIZE_MAX) - newsize = SSIZE_MAX - 1; - else - newsize = newoff; + newsize = off_t_to_size_t(newoff); if (newsize > ms->len) { buf = realloc(*ms->bufp, newsize + 1); if (buf != NULL) { @@ -125,7 +130,7 @@ memstream_seek(void *cookie, off_t pos, case SEEK_SET: /* _fseeko() checks for negative offsets. */ assert(pos >= 0); - ms->offset = pos; + ms->offset = off_t_to_size_t(pos); break; case SEEK_CUR: /* This is only called by _ftello(). */ @@ -153,7 +158,7 @@ memstream_seek(void *cookie, off_t pos, return (-1); } } - ms->offset = ms->len + pos; + ms->offset = off_t_to_size_t(ms->len + pos); break; } memstream_update(ms); Index: src/lib/libc/stdio/open_wmemstream.c diff -u src/lib/libc/stdio/open_wmemstream.c:1.1 src/lib/libc/stdio/open_wmemstream.c:1.2 --- src/lib/libc/stdio/open_wmemstream.c:1.1 Sun Oct 12 20:40:36 2014 +++ src/lib/libc/stdio/open_wmemstream.c Tue Jan 23 10:32:54 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: open_wmemstream.c,v 1.1 2014/10/13 00:40:36 christos Exp $ */ +/* $NetBSD: open_wmemstream.c,v 1.2 2024/01/23 15:32:54 christos Exp $ */ /*- * Copyright (c) 2013 Advanced Computing Technologies LLC @@ -31,7 +31,7 @@ #if 0 __FBSDID("$FreeBSD: head/lib/libc/stdio/open_wmemstream.c 247411 2013-02-27 19:50:46Z jhb $"); #endif -__RCSID("$NetBSD: open_wmemstream.c,v 1.1 2014/10/13 00:40:36 christos Exp $"); +__RCSID("$NetBSD: open_wmemstream.c,v 1.2 2024/01/23 15:32:54 christos Exp $"); #include "namespace.h" #include <assert.h> @@ -52,6 +52,14 @@ struct wmemstream { mbstate_t mbstate; }; +static __inline size_t +off_t_to_size_t(off_t off) +{ + if (off < 0 || off >= SSIZE_MAX) + return SSIZE_MAX - 1; + return (size_t)off; +} + static int wmemstream_grow(struct wmemstream *ms, size_t newoff) { @@ -180,7 +188,7 @@ wmemstream_seek(void *cookie, off_t pos, case SEEK_SET: /* _fseeko() checks for negative offsets. */ assert(pos >= 0); - ms->offset = pos; + ms->offset = off_t_to_size_t(pos); break; case SEEK_CUR: /* This is only called by _ftello(). */ @@ -208,7 +216,7 @@ wmemstream_seek(void *cookie, off_t pos, return (-1); } } - ms->offset = ms->len + pos; + ms->offset = off_t_to_size_t(ms->len + pos); break; } /* Reset the multibyte state if a seek changes the position. */