changeset 105:3b16893a6406

Replace all instances of sprintf with snprintf and adjust size of integer field in some cases
author Jean-Francois Dockes <jf@dockes.org>
date Sat, 31 Dec 2016 11:35:55 +0100
parents f37dd4dea9c1
children dae0b00c1fcb
files Windows/unrtf_w.c src/attr.c src/convert.c src/output.c
diffstat 4 files changed, 27 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/Windows/unrtf_w.c
+++ b/Windows/unrtf_w.c
@@ -102,7 +102,7 @@
     if (buffer[0] == 0) {
         const char *execdir = path_thisexecpath();
         if (strlen(execdir) + strlen("Share") + 4 < MAX_PATH) {
-            sprintf(buffer, "%s\\Share\\", execdir);
+            snprintf(buffer, MAX_PATH, "%s\\Share\\", execdir);
         }
     }
     return buffer;
--- a/src/attr.c
+++ b/src/attr.c
@@ -964,7 +964,7 @@
 assemble_string(char *string, int nr)
 {
 
-	char *s, tmp[12];/* Number of characters that can be in int type (including '\0') - AF */
+	char *s, tmp[20];
 	int i = 0, j = 0;
 
 	if (string == NULL)
@@ -986,7 +986,7 @@
 
 		if (string[i] != '\0')
 		{
-			sprintf(tmp, "%d", nr);
+			snprintf(tmp, 20, "%d", nr);
 			strcpy(&s[j], tmp);
 			j = j + strlen(tmp);
 		}
--- a/src/convert.c
+++ b/src/convert.c
@@ -555,7 +555,7 @@
 }
 
 // Translate code page to encoding name hopefully suitable as iconv input
-static char *cptoencoding(parm)
+static char *cptoencoding(int parm)
 {
 	// Note that CP0 is supposed to mean current system default, which does
 	// not make any sense as a stored value, we don't handle it.
@@ -1383,10 +1383,10 @@
 	}
 	else
 	{
-		sprintf(str, "#%02x%02x%02x",
-		        color_table[num].r,
-		        color_table[num].g,
-		        color_table[num].b);
+		snprintf(str, 40, "#%02x%02x%02x",
+				 color_table[num].r,
+				 color_table[num].g,
+				 color_table[num].b);
 		attr_push(ATTR_FOREGROUND, str);
 	}
 	return FALSE;
@@ -1412,10 +1412,10 @@
 	}
 	else
 	{
-		sprintf(str, "#%02x%02x%02x",
-		        color_table[num].r,
-		        color_table[num].g,
-		        color_table[num].b);
+		snprintf(str, 40, "#%02x%02x%02x",
+				 color_table[num].r,
+				 color_table[num].g,
+				 color_table[num].b);
 		attr_push(ATTR_BACKGROUND, str);
 	}
 	return FALSE;
@@ -1441,7 +1441,7 @@
 	/* Note, fs20 means 10pt */
 	points /= 2;
 
-	sprintf(str, "%d", points);
+	snprintf(str, 20, "%d", points);
 	attr_push(ATTR_FONTSIZE, str);
 
 	return FALSE;
@@ -1630,7 +1630,7 @@
 		{
 			// TOBEDONE: WHAT'S THIS ???
 			name = my_malloc(12);
-			sprintf(name, "%d", num);
+			snprintf(name, 12, "%d", num);
 		}
 
 		/* we are going to output entities, so should not output font */
@@ -1687,7 +1687,7 @@
 	}
 	else
 	{
-		sprintf(str, "#%02x%02x%02x",
+		snprintf(str, 40, "#%02x%02x%02x",
 		        color_table[num].r,
 		        color_table[num].g,
 		        color_table[num].b);
@@ -1855,10 +1855,10 @@
 static int
 cmd_expand(Word *w, int align, char has_param, int param)
 {
-	char str[10];
+	char str[20];
 	if (has_param)
 	{
-		sprintf(str, "%d", param / 4);
+		snprintf(str, 20, "%d", param / 4);
 		if (!param)
 		{
 			attr_pop(ATTR_EXPAND);
@@ -1882,7 +1882,7 @@
 static int
 cmd_emboss(Word *w, int align, char has_param, int param)
 {
-	char str[10];
+	char str[20];
 	if (has_param && !param)
 #ifdef SUPPORT_UNNESTED
 		attr_find_pop(ATTR_EMBOSS);
@@ -1891,7 +1891,7 @@
 #endif
 	else
 	{
-		sprintf(str, "%d", param);
+		snprintf(str, 20, "%d", param);
 		attr_push(ATTR_EMBOSS, str);
 	}
 	return FALSE;
@@ -1908,14 +1908,14 @@
 static int
 cmd_engrave(Word *w, int align, char has_param, int param)
 {
-	char str[10];
+	char str[20];
 	if (has_param && !param)
 	{
 		attr_pop(ATTR_ENGRAVE);
 	}
 	else
 	{
-		sprintf(str, "%d", param);
+		snprintf(str, 20, "%d", param);
 		attr_push(ATTR_ENGRAVE, str);
 	}
 	return FALSE;
@@ -2561,7 +2561,7 @@
 
 	short	done = 0;
 	long unicode_number = (long) param; /* On 16bit architectures int is too small to store unicode characters. - AF */
-	char tmp[12]; /* Number of characters that can be in int type (including '\0'). If int size is greater than 4 bytes change this value. - AF */
+	char tmp[20]; /* Number of characters that can be in int type (including '\0'). If int size is greater than 4 bytes change this value. - AF */
 	const char *alias;
 #define DEBUG 0
 #if DEBUG
@@ -2590,7 +2590,7 @@
 			/* RTF spec: Unicode values beyond 32767 are represented by negative numbers */
 			unicode_number += 65536;
 		}
-		sprintf(tmp, "%ld", unicode_number);
+		snprintf(tmp, 20, "%ld", unicode_number);
 
 		if (safe_printf(1, op->unisymbol_print, tmp))
 		{
@@ -4214,8 +4214,8 @@
 								ext = "emf";
 								break;  /* Enhanced MetaFile */
 							}
-							sprintf(picture_path, "pict%03d.%s",
-							        picture_file_number++, ext);
+							snprintf(picture_path, 255, "pict%03d.%s",
+									 picture_file_number++, ext);
 							pictfile = fopen(picture_path, "wb");
 						}
 
--- a/src/output.c
+++ b/src/output.c
@@ -402,7 +402,7 @@
 		if (op->fontsize_begin)
 		{
 			char expr[16];
-			sprintf(expr, "%d", size);
+			snprintf(expr, 16, "%d", size);
 			if (safe_printf(1, op->fontsize_begin, expr))
 			{
 				fprintf(stderr, TOO_MANY_ARGS, "fontsize_begin");
@@ -629,7 +629,7 @@
 		if (op->fontsize_end)
 		{
 			char expr[16];
-			sprintf(expr, "%d", size);
+			snprintf(expr, 16, "%d", size);
 			if (safe_printf(1, op->fontsize_end, expr))
 			{
 				fprintf(stderr, TOO_MANY_ARGS, "fontsize_end");