From ec5c1c0446e793215b7045834fd1cb71fc98ea5f Mon Sep 17 00:00:00 2001 From: op Date: Wed, 28 Aug 2024 11:40:33 +0000 Subject: [PATCH] libpcap: replace hand-rolled number parser with strtol can't use strtonum here since it needs to handle octal and hex notations as well. Part of a larger diff that's ok beck@ --- lib/libpcap/scanner.l | 48 ++++++++++++++----------------------------- 1 file changed, 15 insertions(+), 33 deletions(-) diff --git a/lib/libpcap/scanner.l b/lib/libpcap/scanner.l index 6ee53082379..a738b252ba1 100644 --- a/lib/libpcap/scanner.l +++ b/lib/libpcap/scanner.l @@ -1,5 +1,5 @@ %{ -/* $OpenBSD: scanner.l,v 1.30 2024/04/08 02:51:14 jsg Exp $ */ +/* $OpenBSD: scanner.l,v 1.31 2024/08/28 11:40:33 op Exp $ */ /* * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997 @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -47,7 +48,6 @@ #include "grammar.h" static int stoi(char *); -static inline int xdtoi(int); #ifdef FLEX_SCANNER #define YY_NO_UNPUT @@ -333,41 +333,23 @@ yywrap(void) return 1; } -/* Hex digit to integer. */ -static inline int -xdtoi(int c) -{ - if (isdigit(c)) - return c - '0'; - else if (islower(c)) - return c - 'a' + 10; - else - return c - 'A' + 10; -} - /* - * Convert string to integer. Just like atoi(), but checks for - * preceding 0x or 0 and uses hex or octal instead of decimal. + * Convert string to integer supporting also octal and hex notations. */ static int stoi(char *s) { - int base = 10; - int n = 0; - - if (*s == '0') { - if (s[1] == 'x' || s[1] == 'X') { - s += 2; - base = 16; - } - else { - base = 8; - s += 1; - } - } - while (*s) - n = n * base + xdtoi(*s++); - - return n; + long lval; + char *ep; + + errno = 0; + lval = strtol(s, &ep, 0); + if (*s == '\0' || *ep == '\0') + bpf_error("invalid number %s", s); + if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) || + (lval > INT_MAX || lval < INT_MIN)) + bpf_error("out of range: %s", s); + + return lval; } -- 2.20.1