#983823 macro.c: add a cast to "ptrdiff_t", use "snprintf()", and make an "unsigned char" cast

Package:
src:nn
Source:
nn
Submitter:
Bjarni Ingi Gislason
Date:
2021-03-02 01:18:04 UTC
Severity:
normal
Tags:
#983823#5
Date:
2021-03-02 01:15:55 UTC
From:
To:
Dear Maintainer,

  Add <stddef.h> and a cast to "ptrdiff_t" to avoid a warning:

macro.c: In function 'm_define':
macro.c:412:48: warning: cast to pointer from integer of different size
[-Wint-to-pointer-cast]
  412 |     return type == 0 ? (char *) macro[cur_m] : (char *) (K_MACRO | cur_m);
      |                                                ^

  Use "snprintf()" instead of "sprintf()".

  Add information to the output of "m_error()".

  Cast an integer variable to unsigned character for the function
"isspace()".

Signed-off-by: Bjarni Ingi Gislason <bjarniig@rhi.hi.is>
---
 macro.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/macro.c b/macro.c
index 4037d60..231fe9a 100644
--- a/macro.c
+++ b/macro.c
@@ -5,6 +5,7 @@
  *	Macro parsing and execution.
  */

+#include <stddef.h> /* For definition of "ptrdiff_t" */
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
@@ -105,10 +106,11 @@ static int      cur_m;
 static void
 m_error(char *fmt, char *arg)
 {
-    char            buf[80];
+#define    NBUF  80
+    char            buf[NBUF];

     if (arg) {
-	sprintf(buf, fmt, arg);
+	snprintf(buf, NBUF, fmt, arg);
 	fmt = buf;
     }
     init_message("Error in macro %d: %s", cur_m, fmt);
@@ -409,7 +411,7 @@ out:
     if (f == stdin)
 	nn_raw();
     m = NULL;
-    return type == 0 ? (char *) macro[cur_m] : (char *) (K_MACRO | cur_m);
+    return type == 0 ? (char *) macro[cur_m] : (char *) ((ptrdiff_t) (K_MACRO | cur_m));
 }

 static char    *
@@ -418,7 +420,7 @@ m_get_macro(char *id)
     if (id) {
 	cur_m = atoi(id);
 	if (cur_m < 0 || cur_m >= NMACRO) {
-	    m_error("macro number out of range\n", id);
+	    m_error("macro number %s is out of range\n", id);
 	    return (char *) 0;
 	}
     }
@@ -433,7 +435,7 @@ parse_enter_macro(FILE * f, register int c)
     group_header   *gh;
     static char    *last_defined = NULL;

-    while (c != EOF && c != NL && (!isascii(c) || isspace(c)))
+    while (c != EOF && c != NL && (!isascii(c) || isspace((unsigned char) c)))
 	c = getc(f);

     if (c == ')')
@@ -449,7 +451,7 @@ parse_enter_macro(FILE * f, register int c)
     do {
 	*gp++ = c;
 	c = getc(f);
-    } while (c != EOF && c != ')' && isascii(c) && !isspace(c));
+    } while (c != EOF && c != ')' && isascii(c) && !isspace((unsigned char) c));

     *gp = NUL;
     if ((gh = lookup(other)))