examples: don't promote unchecked function calls

Akim Demaille <[email protected]>
Newsgroups gmane.comp.parsers.bison.patches
Message-ID <[email protected]>
commit 4619b32dc079b7833a784d788114cb1129647723
Author: Akim Demaille <[email protected]>
Date:   Tue May 12 19:03:29 2020 +0200

    examples: don't promote unchecked function calls
    
    * etc/bench.pl.in, examples/c/bistromathic/parse.y,
    * examples/c/calc/calc.y, examples/c/pushcalc/calc.y: Check scanf's
    return value.
    * doc/bison.texi: Likewise, but only for the second example, to avoid
    cluttering the very simple case.

diff --git a/TODO b/TODO
index 006284eb..d8f175f8 100644
--- a/TODO
+++ b/TODO
@@ -7,6 +7,8 @@
 - How about not evaluating incomplete lines when the text is not finished
   (as shells do).
 
+- Caret diagnostics
+
 ** Doc
 *** api.header.include
 
diff --git a/doc/bison.texi b/doc/bison.texi
index 726cbf22..15c9b1c0 100644
--- a/doc/bison.texi
+++ b/doc/bison.texi
@@ -2656,6 +2656,7 @@ found, a pointer to that symbol is returned; otherwise zero is returned.
    always succeed, and that integer calculations
    never overflow.  Production-quality code should
    not make these assumptions.  */
+#include <assert.h>
 #include <stdlib.h> /* malloc, realloc. */
 #include <string.h> /* strlen. */
 @end group
@@ -2728,7 +2729,8 @@ yylex (void)
   if (c == '.' || isdigit (c))
     @{
       ungetc (c, stdin);
-      scanf ("%lf", &yylval.NUM);
+      int n = scanf ("%lf", &yylval.NUM);
+      assert (n == 1);
       return NUM;
     @}
 @end group
diff --git a/etc/bench.pl.in b/etc/bench.pl.in
index b7d44f41..036bf52e 100755
--- a/etc/bench.pl.in
+++ b/etc/bench.pl.in
@@ -498,7 +498,8 @@ yylex (void)
     case '5': case '6': case '7': case '8': case '9':
       {
         int nchars = 0;
-        sscanf (input - 1, "%d%n", &yylval.NUM, &nchars);
+        int n = sscanf (input - 1, "%d%n", &yylval.NUM, &nchars);
+        assert (n == 1);
         input += nchars - 1;
         return NUM;
       }
diff --git a/examples/c/bistromathic/parse.y b/examples/c/bistromathic/parse.y
index c2ddb124..78ec6eb7 100644
--- a/examples/c/bistromathic/parse.y
+++ b/examples/c/bistromathic/parse.y
@@ -268,7 +268,8 @@ yylex (const char **line, YYSTYPE *yylval, YYLTYPE *yylloc)
     case '5': case '6': case '7': case '8': case '9':
       {
         int nchars = 0;
-        sscanf (*line - 1, "%lf%n", &yylval->TOK_NUM, &nchars);
+        int n = sscanf (*line - 1, "%lf%n", &yylval->TOK_NUM, &nchars);
+        assert (n == 1);
         *line += nchars - 1;
         yylloc->last_column += nchars - 1;
         return TOK_NUM;
@@ -284,7 +285,8 @@ yylex (const char **line, YYSTYPE *yylval, YYLTYPE *yylloc)
       {
         int nchars = 0;
         char buf[100];
-        sscanf (*line - 1, "%99[a-z]%n", buf, &nchars);
+        int n = sscanf (*line - 1, "%99[a-z]%n", buf, &nchars);
+        assert (n == 1);
         *line += nchars - 1;
         yylloc->last_column += nchars - 1;
         if (strcmp (buf, "exit") == 0)
diff --git a/examples/c/calc/calc.y b/examples/c/calc/calc.y
index 7757648d..ff571114 100644
--- a/examples/c/calc/calc.y
+++ b/examples/c/calc/calc.y
@@ -1,4 +1,5 @@
 %code top {
+  #include <assert.h>
   #include <ctype.h>  /* isdigit. */
   #include <stdio.h>  /* For printf, etc. */
   #include <string.h> /* strcmp. */
@@ -73,7 +74,8 @@ yylex (void)
   if (c == '.' || isdigit (c))
     {
       ungetc (c, stdin);
-      scanf ("%lf", &yylval.NUM);
+      int n = scanf ("%lf", &yylval.NUM);
+      assert (n == 1);
       return NUM;
     }
 
diff --git a/examples/c/pushcalc/calc.y b/examples/c/pushcalc/calc.y
index 7b0b9996..bc27ae95 100644
--- a/examples/c/pushcalc/calc.y
+++ b/examples/c/pushcalc/calc.y
@@ -1,6 +1,6 @@
 %code top {
+  #include <assert.h>
   #include <ctype.h>  /* isdigit. */
-  #include <stdbool.h>
   #include <stdio.h>  /* For printf, etc. */
   #include <string.h> /* strcmp. */
 }
@@ -81,7 +81,8 @@ yylex (YYSTYPE *yylval)
   if (c == '.' || isdigit (c))
     {
       ungetc (c, stdin);
-      scanf ("%lf", &yylval->NUM);
+      int n = scanf ("%lf", &yylval->NUM);
+      assert (n == 1);
       return NUM;
     }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.