error: clean up comparison of word vectors

The function wordvcmp returned -1 when either of the word vectors was
short; this made the function asymmetric. Since the function is only
used to compare two word vectors for equality, restrict it to this
particular use case.
This commit is contained in:
rillig 2023-08-26 15:18:27 +00:00
parent 1f3ad203f7
commit 08ac0fb3d4
4 changed files with 31 additions and 35 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: error.h,v 1.20 2023/08/26 14:50:53 rillig Exp $ */
/* $NetBSD: error.h,v 1.21 2023/08/26 15:18:27 rillig Exp $ */
/*
* Copyright (c) 1980, 1993
@ -281,6 +281,6 @@ char *substitute(char *, char, char);
bool touchfiles(int, Eptr **, int *, char ***);
const char *verbform(int);
void wordvbuild(char *, int*, char ***);
int wordvcmp(char **, int, char **);
bool wordv_eq(char **, int, char **);
void wordvprint(FILE *, int, char **);
char **wordvsplice(int, int, char **);

View File

@ -1,4 +1,4 @@
/* $NetBSD: input.c,v 1.20 2023/08/26 14:50:53 rillig Exp $ */
/* $NetBSD: input.c,v 1.21 2023/08/26 15:18:27 rillig Exp $ */
/*
* Copyright (c) 1980, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)input.c 8.1 (Berkeley) 6/6/93";
#endif
__RCSID("$NetBSD: input.c,v 1.20 2023/08/26 14:50:53 rillig Exp $");
__RCSID("$NetBSD: input.c,v 1.21 2023/08/26 15:18:27 rillig Exp $");
#endif /* not lint */
#include <stdio.h>
@ -488,8 +488,8 @@ lint3(void)
{
if (cur_wordc < 3)
return C_UNKNOWN;
if (wordvcmp(cur_wordv+2, 4, Lint31) == 0
|| wordvcmp(cur_wordv+2, 6, Lint32) == 0) {
if (wordv_eq(cur_wordv+2, 4, Lint31)
|| wordv_eq(cur_wordv+2, 6, Lint32)) {
language = INLINT;
return C_NONSPEC;
}
@ -526,7 +526,7 @@ f77(void)
* Warning on line %d of %s: %s
* Error. No assembly.
*/
if (cur_wordc == 3 && wordvcmp(cur_wordv+1, 3, F77_no_ass) == 0) {
if (cur_wordc == 3 && wordv_eq(cur_wordv+1, 3, F77_no_ass)) {
cur_wordc = 0;
return C_SYNC;
}
@ -534,9 +534,9 @@ f77(void)
return C_UNKNOWN;
if (lastchar(cur_wordv[6]) == ':'
&& (
wordvcmp(cur_wordv+1, 3, F77_fatal) == 0
|| wordvcmp(cur_wordv+1, 3, F77_error) == 0
|| wordvcmp(cur_wordv+1, 3, F77_warning) == 0
wordv_eq(cur_wordv+1, 3, F77_fatal)
|| wordv_eq(cur_wordv+1, 3, F77_error)
|| wordv_eq(cur_wordv+1, 3, F77_warning)
)
) {
language = INF77;
@ -563,11 +563,11 @@ DECL_STRINGS_5(static, Make_NotRemade,
static Errorclass
make(void)
{
if (wordvcmp(cur_wordv+1, 3, Make_Croak) == 0) {
if (wordv_eq(cur_wordv+1, 3, Make_Croak)) {
language = INMAKE;
return C_SYNC;
}
if (wordvcmp(cur_wordv+2, 5, Make_NotRemade) == 0) {
if (wordv_eq(cur_wordv+2, 5, Make_NotRemade)) {
language = INMAKE;
return C_SYNC;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: pi.c,v 1.23 2023/08/26 14:59:44 rillig Exp $ */
/* $NetBSD: pi.c,v 1.24 2023/08/26 15:18:27 rillig Exp $ */
/*
* Copyright (c) 1980, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)pi.c 8.1 (Berkeley) 6/6/93";
#endif
__RCSID("$NetBSD: pi.c,v 1.23 2023/08/26 14:59:44 rillig Exp $");
__RCSID("$NetBSD: pi.c,v 1.24 2023/08/26 15:18:27 rillig Exp $");
#endif /* not lint */
#include <stdio.h>
@ -329,10 +329,10 @@ pi(void)
int wordindex;
language = INPI;
if ((undefined = (wordvcmp(cur_wordv+2, 3, pi_und1) == 0))
|| (undefined = (wordvcmp(cur_wordv+2, 3, pi_und2) == 0))
|| wordvcmp(cur_wordv+2, 4, pi_imp1) == 0
|| wordvcmp(cur_wordv+2, 4, pi_imp2) == 0
if ((undefined = wordv_eq(cur_wordv+2, 3, pi_und1))
|| (undefined = wordv_eq(cur_wordv+2, 3, pi_und2))
|| wordv_eq(cur_wordv+2, 4, pi_imp1)
|| wordv_eq(cur_wordv+2, 4, pi_imp2)
) {
for (wordindex = undefined ? 5 : 6;
wordindex <= cur_wordc;
@ -419,10 +419,10 @@ pi(void)
structured = false;
multiple = false;
if (
(cur_wordc == 6 && wordvcmp(cur_wordv+1, 2, pi_Endmatched) == 0)
|| (cur_wordc == 8 && wordvcmp(cur_wordv+1, 4, pi_Inserted) == 0)
|| (multiple = (cur_wordc == 9 && wordvcmp(cur_wordv+1,6, pi_multiple) == 0))
|| (structured = (cur_wordc == 10 && wordvcmp(cur_wordv+6,5, pi_structured) == 0))
(cur_wordc == 6 && wordv_eq(cur_wordv+1, 2, pi_Endmatched))
|| (cur_wordc == 8 && wordv_eq(cur_wordv+1, 4, pi_Inserted))
|| (multiple = (cur_wordc == 9 && wordv_eq(cur_wordv+1,6, pi_multiple)))
|| (structured = (cur_wordc == 10 && wordv_eq(cur_wordv+6,5, pi_structured)))
) {
language = INPI;
nwordv = wordvsplice(2, cur_wordc, cur_wordv+1);

View File

@ -1,4 +1,4 @@
/* $NetBSD: subr.c,v 1.25 2023/08/26 15:07:14 rillig Exp $ */
/* $NetBSD: subr.c,v 1.26 2023/08/26 15:18:27 rillig Exp $ */
/*
* Copyright (c) 1980, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "@(#)subr.c 8.1 (Berkeley) 6/6/93";
#endif
__RCSID("$NetBSD: subr.c,v 1.25 2023/08/26 15:07:14 rillig Exp $");
__RCSID("$NetBSD: subr.c,v 1.26 2023/08/26 15:18:27 rillig Exp $");
#endif /* not lint */
#include <ctype.h>
@ -347,18 +347,14 @@ wordvbuild(char *string, int *r_wordc, char ***r_wordv)
/*
* Compare two 0 based wordvectors
*/
int
wordvcmp(char **wordv1, int wordc, char **wordv2)
bool
wordv_eq(char **wordv1, int wordc, char **wordv2)
{
int back;
for (int i = 0; i < wordc; i++) {
if (wordv1[i] == NULL || wordv2[i] == NULL)
return -1;
if ((back = strcmp(wordv1[i], wordv2[i])) != 0)
return back;
}
return 0; /* they are equal */
for (int i = 0; i < wordc; i++)
if (wordv1[i] == NULL || wordv2[i] == NULL
|| strcmp(wordv1[i], wordv2[i]) != 0)
return false;
return true;
}
/*