Check that #ifdef, #ifndef, and #undef are used with a single name

This restores the parameter validation that has been peformed by cpp
for defining and testing of names used in conditions.

MFC after:	3 days
This commit is contained in:
Stefan Eßer 2020-11-03 11:37:19 +00:00
parent 0cadbd6cd2
commit cc9fbfafbe

View File

@ -89,22 +89,29 @@ static StringList *definitions = NULL;
static struct event *events[MAXCOUNT]; static struct event *events[MAXCOUNT];
static char *extradata[MAXCOUNT]; static char *extradata[MAXCOUNT];
static void static char *
trimlr(char **buf) trimlr(char **buf)
{ {
char *walk = *buf; char *walk = *buf;
char *sep;
char *last; char *last;
while (isspace(*walk)) while (isspace(*walk))
walk++; walk++;
if (*walk != '\0') { *buf = walk;
last = walk + strlen(walk) - 1;
sep = walk;
while (*sep != '\0' && !isspace(*sep))
sep++;
if (*sep != '\0') {
last = sep + strlen(sep) - 1;
while (last > walk && isspace(*last)) while (last > walk && isspace(*last))
last--; last--;
*(last+1) = 0; *(last+1) = 0;
} }
*buf = walk; return (sep);
} }
static FILE * static FILE *
@ -167,7 +174,7 @@ cal_path(void)
static int static int
token(char *line, FILE *out, int *skip, int *unskip) token(char *line, FILE *out, int *skip, int *unskip)
{ {
char *walk, c, a; char *walk, *sep, a, c;
const char *this_cal_home; const char *this_cal_home;
const char *this_cal_dir; const char *this_cal_dir;
const char *this_cal_file; const char *this_cal_file;
@ -188,14 +195,20 @@ token(char *line, FILE *out, int *skip, int *unskip)
if (strncmp(line, "ifdef", 5) == 0) { if (strncmp(line, "ifdef", 5) == 0) {
walk = line + 5; walk = line + 5;
trimlr(&walk); sep = trimlr(&walk);
if (*walk == '\0') { if (*walk == '\0') {
WARN0("Expecting arguments after #ifdef"); WARN0("Expecting arguments after #ifdef");
return (T_ERR); return (T_ERR);
} }
if (*sep != '\0') {
WARN1("Expecting a single word after #ifdef "
"but got \"%s\"", walk);
return (T_ERR);
}
if (*skip != 0 || definitions == NULL || sl_find(definitions, walk) == NULL) if (*skip != 0 ||
definitions == NULL || sl_find(definitions, walk) == NULL)
++*skip; ++*skip;
else else
++*unskip; ++*unskip;
@ -205,14 +218,20 @@ token(char *line, FILE *out, int *skip, int *unskip)
if (strncmp(line, "ifndef", 6) == 0) { if (strncmp(line, "ifndef", 6) == 0) {
walk = line + 6; walk = line + 6;
trimlr(&walk); sep = trimlr(&walk);
if (*walk == '\0') { if (*walk == '\0') {
WARN0("Expecting arguments after #ifndef"); WARN0("Expecting arguments after #ifndef");
return (T_ERR); return (T_ERR);
} }
if (*sep != '\0') {
WARN1("Expecting a single word after #ifndef "
"but got \"%s\"", walk);
return (T_ERR);
}
if (*skip != 0 || (definitions != NULL && sl_find(definitions, walk) != NULL)) if (*skip != 0 ||
(definitions != NULL && sl_find(definitions, walk) != NULL))
++*skip; ++*skip;
else else
++*unskip; ++*unskip;
@ -222,7 +241,7 @@ token(char *line, FILE *out, int *skip, int *unskip)
if (strncmp(line, "else", 4) == 0) { if (strncmp(line, "else", 4) == 0) {
walk = line + 4; walk = line + 4;
trimlr(&walk); (void)trimlr(&walk);
if (*walk != '\0') { if (*walk != '\0') {
WARN0("Expecting no arguments after #else"); WARN0("Expecting no arguments after #else");
@ -251,7 +270,7 @@ token(char *line, FILE *out, int *skip, int *unskip)
if (strncmp(line, "include", 7) == 0) { if (strncmp(line, "include", 7) == 0) {
walk = line + 7; walk = line + 7;
trimlr(&walk); (void)trimlr(&walk);
if (*walk == '\0') { if (*walk == '\0') {
WARN0("Expecting arguments after #include"); WARN0("Expecting arguments after #include");
@ -291,7 +310,8 @@ token(char *line, FILE *out, int *skip, int *unskip)
if (definitions == NULL) if (definitions == NULL)
definitions = sl_init(); definitions = sl_init();
walk = line + 6; walk = line + 6;
trimlr(&walk); sep = trimlr(&walk);
*sep = '\0';
if (*walk == '\0') { if (*walk == '\0') {
WARN0("Expecting arguments after #define"); WARN0("Expecting arguments after #define");
@ -306,12 +326,17 @@ token(char *line, FILE *out, int *skip, int *unskip)
if (strncmp(line, "undef", 5) == 0) { if (strncmp(line, "undef", 5) == 0) {
if (definitions != NULL) { if (definitions != NULL) {
walk = line + 5; walk = line + 5;
trimlr(&walk); sep = trimlr(&walk);
if (*walk == '\0') { if (*walk == '\0') {
WARN0("Expecting arguments after #undef"); WARN0("Expecting arguments after #undef");
return (T_ERR); return (T_ERR);
} }
if (*sep != '\0') {
WARN1("Expecting a single word after #undef "
"but got \"%s\"", walk);
return (T_ERR);
}
walk = sl_find(definitions, walk); walk = sl_find(definitions, walk);
if (walk != NULL) if (walk != NULL)