From nobody Fri Jun 27 15:16:10 2025 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4bTJzb55cDz5ytqY; Fri, 27 Jun 2025 15:16:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4bTJzZ6nQTz3YP2; Fri, 27 Jun 2025 15:16:10 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1751037371; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=M/cKC2uPe76Pn27cecNQGOvd6aHCX45j5fO0I/lJkeE=; b=Zk4oH7vmkMe3iEJDPSyFvzjcXlAjGsJP2ObmJFPF78VS/z6Lno2K8dTAQ/oVfPZk5L9E3v iR/e8E6Xfv5IkMvxr4yqrc82fPJGI82jCADGGDX060vVpHFYsJWsP1zX8tXpwBmyjOuLs3 YovK/cJ/kW3nDj8lxSlW2mF4saJ2v/+PA5Y68oxdD66LXOqZ8Xntle/1g+EAEkzMmmlmum msvMzRi+ZWJEUDbFYqfr4X+fv3VgKjLI5enoqSZXXgCXXWjI3bV0Q1yBgB3lorU8fEXgfp 3TvI42AB1FSF2KxQjq8Q2cLT42xWKJTOwO7Jn306m7mIBiMXFTPl2rllHP4igg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1751037371; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=M/cKC2uPe76Pn27cecNQGOvd6aHCX45j5fO0I/lJkeE=; b=h6/U3WADI4rcl3lasRu08j77viBIfOtj5olD/ueDkS6PVDlBCVYNXXcAzWjxC/pXtcU5Dn UwR2nbsfXcXfTvuE8N7VjglXTk1zISOmiQTOxBegc2iPW8wDnhzaPSv1JJlylPiqkp0LDR rRl7Fiia78ox5q9JwN85b/ik508ldEE3N5VEfuQmboe0gCbJx+6KghtMljXmCjyUPenu1Z Qv8lfgWBgMHRt5K2/wSARhC//YUKBJAoGtVvzHEEBQKn3Kx6TK5xPuCWK55MJeG+bGg8X4 rk+isfr6DxRBv0jHbWvZqzNaU2j4CHtM3rFiWHi/E13Cy5XzwuzIgl5c5ySoLg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1751037371; a=rsa-sha256; cv=none; b=gIQrx0M1BAzKNZxN1A7ACwNsJ1yNOgkRzxX5IF5pgptByP3I6O8M4KADeeNctqfU79IjqA EwdUSGPox4peOo8gAb47hgEl0/juulhk5kiHEw7NnZpXFng9kzl9NKCXoe2aDqmXqQbaA4 qcFLOnnjwVbsWMHJMMwQybv2v8SreP/6aVwoJEUCHfb93Ao6Kkezsdn5qpyAdGjUEtVBLY bbWoZhlYcF/BrE/xAXSbhtnR6f6MalKXsTPXm+iLlPbKqtT8EhshjCRLXMXqGXFZJFHDqr yEGdBRwtQ9ZhkHgwkPn1osvDccUsgaReHaDF77WbtIme5s217rZMkptqttff/g== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4bTJzZ6P8GztWt; Fri, 27 Jun 2025 15:16:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 55RFGAFD049049; Fri, 27 Jun 2025 15:16:10 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 55RFGA6M049046; Fri, 27 Jun 2025 15:16:10 GMT (envelope-from git) Date: Fri, 27 Jun 2025 15:16:10 GMT Message-Id: <202506271516.55RFGA6M049046@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kristof Provost Subject: git: d40166400955 - main - pfctl: fix reporting of line numbers for included files List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: d401664009554167bfc3c1cdb51aba8d1065e92c Auto-Submitted: auto-generated The branch main has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=d401664009554167bfc3c1cdb51aba8d1065e92c commit d401664009554167bfc3c1cdb51aba8d1065e92c Author: Kristof Provost AuthorDate: 2025-06-23 13:17:17 +0000 Commit: Kristof Provost CommitDate: 2025-06-27 14:55:16 +0000 pfctl: fix reporting of line numbers for included files Fix (include file ) line number counting by keeping track of the unget buffer per file; also use that buffer to expand macros. ok sashan@ dennis@ krw@ Obtained from: OpenBSD, otto , 475a466cc5 Sponsored by: Rubicon Communications, LLC ("Netgate") --- sbin/pfctl/parse.y | 122 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 75 insertions(+), 47 deletions(-) diff --git a/sbin/pfctl/parse.y b/sbin/pfctl/parse.y index 257a62df76f4..e4cd4883e2c9 100644 --- a/sbin/pfctl/parse.y +++ b/sbin/pfctl/parse.y @@ -89,6 +89,10 @@ static struct file { TAILQ_ENTRY(file) entry; FILE *stream; char *name; + size_t ungetpos; + size_t ungetsize; + u_char *ungetbuf; + int eof_reached; int lineno; int errors; } *file; @@ -100,8 +104,9 @@ int yylex(void); int yyerror(const char *, ...); int kw_cmp(const void *, const void *); int lookup(char *); +int igetc(void); int lgetc(int); -int lungetc(int); +void lungetc(int); int findeol(void); static TAILQ_HEAD(symhead, sym) symhead = TAILQ_HEAD_INITIALIZER(symhead); @@ -6825,34 +6830,37 @@ lookup(char *s) } } -#define MAXPUSHBACK 128 +#define START_EXPAND 1 +#define DONE_EXPAND 2 -static char *parsebuf; -static int parseindex; -static char pushback_buffer[MAXPUSHBACK]; -static int pushback_index = 0; +static int expanding; int -lgetc(int quotec) +igetc(void) { - int c, next; - - if (parsebuf) { - /* Read character from the parsebuffer instead of input. */ - if (parseindex >= 0) { - c = parsebuf[parseindex++]; - if (c != '\0') - return (c); - parsebuf = NULL; - } else - parseindex++; + int c; + while (1) { + if (file->ungetpos > 0) + c = file->ungetbuf[--file->ungetpos]; + else + c = getc(file->stream); + if (c == START_EXPAND) + expanding = 1; + else if (c == DONE_EXPAND) + expanding = 0; + else + break; } + return (c); +} - if (pushback_index) - return (pushback_buffer[--pushback_index]); +int +lgetc(int quotec) +{ + int c, next; if (quotec) { - if ((c = getc(file->stream)) == EOF) { + if ((c = igetc()) == EOF) { yyerror("reached end of file while parsing quoted string"); if (popfile() == EOF) return (EOF); @@ -6861,8 +6869,8 @@ lgetc(int quotec) return (c); } - while ((c = getc(file->stream)) == '\\') { - next = getc(file->stream); + while ((c = igetc()) == '\\') { + next = igetc(); if (next != '\n') { c = next; break; @@ -6871,28 +6879,38 @@ lgetc(int quotec) file->lineno++; } - while (c == EOF) { - if (popfile() == EOF) - return (EOF); - c = getc(file->stream); + if (c == EOF) { + /* + * Fake EOL when hit EOF for the first time. This gets line + * count right if last line in included file is syntactically + * invalid and has no newline. + */ + if (file->eof_reached == 0) { + file->eof_reached = 1; + return ('\n'); + } + while (c == EOF) { + if (popfile() == EOF) + return (EOF); + c = igetc(); + } } return (c); } -int +void lungetc(int c) { if (c == EOF) - return (EOF); - if (parsebuf) { - parseindex--; - if (parseindex >= 0) - return (c); + return; + if (file->ungetpos >= file->ungetsize) { + void *p = reallocarray(file->ungetbuf, file->ungetsize, 2); + if (p == NULL) + err(1, "lungetc"); + file->ungetbuf = p; + file->ungetsize *= 2; } - if (pushback_index < MAXPUSHBACK-1) - return (pushback_buffer[pushback_index++] = c); - else - return (EOF); + file->ungetbuf[file->ungetpos++] = c; } int @@ -6900,14 +6918,9 @@ findeol(void) { int c; - parsebuf = NULL; - /* skip to either EOF or the first real EOL */ while (1) { - if (pushback_index) - c = pushback_buffer[--pushback_index]; - else - c = lgetc(0); + c = lgetc(0); if (c == '\n') { file->lineno++; break; @@ -6935,7 +6948,7 @@ top: if (c == '#') while ((c = lgetc(0)) != '\n' && c != EOF) ; /* nothing */ - if (c == '$' && parsebuf == NULL) { + if (c == '$' && !expanding) { while (1) { if ((c = lgetc(0)) == EOF) return (0); @@ -6957,8 +6970,13 @@ top: yyerror("macro '%s' not defined", buf); return (findeol()); } - parsebuf = val; - parseindex = 0; + p = val + strlen(val) - 1; + lungetc(DONE_EXPAND); + while (p >= val) { + lungetc(*p); + p--; + } + lungetc(START_EXPAND); goto top; } @@ -7148,7 +7166,16 @@ pushfile(const char *name, int secret) free(nfile); return (NULL); } - nfile->lineno = 1; + nfile->lineno = TAILQ_EMPTY(&files) ? 1 : 0; + nfile->ungetsize = 16; + nfile->ungetbuf = malloc(nfile->ungetsize); + if (nfile->ungetbuf == NULL) { + warn("malloc"); + fclose(nfile->stream); + free(nfile->name); + free(nfile); + return (NULL); + } TAILQ_INSERT_TAIL(&files, nfile, entry); return (nfile); } @@ -7163,6 +7190,7 @@ popfile(void) TAILQ_REMOVE(&files, file, entry); fclose(file->stream); free(file->name); + free(file->ungetbuf); free(file); file = prev; return (0);