1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
From: Ben Hutchings <benh@debian.org>
Date: Sat, 27 Jul 2024 01:32:06 +0200
Subject: Add format attributes to allow type checks on *printf() wrappers
Forwarded: not-needed as there is no upstream anymore
Varargs functions normally can't be type-checked. However gcc can
parse printf()-style format strings and check the following arguments
against them. Declare fatal(), yyerror(), and yywarning() with the
required function attribute.
Adding this will trigger new errors:
main.c:82:25: error: format not a string literal and no format arguments [-Werror=format-security]
82 | fatal(usage);
| ^~~~~
main.c:85:9: error: format not a string literal and no format arguments [-Werror=format-security]
85 | if(input_file == NULL) fatal(usage);
| ^~
To avoid that, change the 'usage' variable to be a const array and
change the format parameters of these functions to be const-qualified.
---
--- a/a56.h
+++ b/a56.h
@@ -95,7 +95,7 @@ struct psect {
/* gram.c (from a56.y) */
char *tok_print(int tok);
-void yyerror(char *s, ...);
+void yyerror(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
/* kparse.c (from keybld.c) */
int kparse(char *kp);
@@ -122,7 +122,7 @@ struct psect *new_psect(char *name, int
FILE *open_read(char *file);
FILE *open_write(char *file);
FILE *open_append(char *file);
-void fatal(char *c, ...);
+void fatal(const char *c, ...) __attribute__ ((format (printf, 1, 2)));
char *alloc(int size);
char *untab(char *s);
char *untabn(char *s, int stops);
--- a/a56.y
+++ b/a56.y
@@ -70,7 +70,7 @@ struct n binary_op(struct n a1, int op,
struct n unary_op(int op, struct n a1);
int n2int(struct n n);
int n2frac(struct n n);
-void yywarning(char *s, ...);
+void yywarning(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
char *luntab(char *s);
#define R_R6 0x0001
@@ -2056,7 +2056,7 @@ char *tok_print(int tok)
return "*bogus*";
}
-void yyerror(char *s, ...)
+void yyerror(const char *s, ...)
{
extern int error;
char buf[1024];
@@ -2076,7 +2076,7 @@ void yyerror(char *s, ...)
}
}
-void yywarning(char *s, ...)
+void yywarning(const char *s, ...)
{
extern int warning;
char buf[1024];
--- a/main.c
+++ b/main.c
@@ -60,7 +60,7 @@ int main(int argc, char *argv[])
int c;
char *output_file = "a56.out";
char *input_file;
- char *usage = "usage: a56 [-b] [-l] [-d] [-o output-file] input-file\n";
+ const char usage[] = "usage: a56 [-b] [-l] [-d] [-o output-file] input-file\n";
while((c = getopt(argc, argv, "bldo:")) != EOF) switch (c) {
case 'b':
--- a/subs.c
+++ b/subs.c
@@ -65,7 +65,7 @@ FILE *open_append(char *file)
return fp;
}
-void fatal(char *c, ...)
+void fatal(const char *c, ...)
{
va_list ap;
|