Blob Blame History Raw
From 7b975696a7bda5b86fcf168644f177544adb6fe9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Petr=20Men=C5=A1=C3=ADk?= <pemensik@redhat.com>
Date: Fri, 3 Sep 2021 17:38:26 +0200
Subject: [PATCH 09/15] Address coverity issues detected in util.c

Error: CLANG_WARNING: [#def163]
dnsmasq-2.86test7/src/util.c:204:9: warning[deadcode.DeadStores]: Although the value stored to 'rc' is used in the enclosing expression, the value is never actually read from 'rc'
 #  202|       *nomem = 0;
 #  203|
 #  204|->   if (!(rc = check_name(in)))
 #  205|       return NULL;
 #  206|

Error: UNREACHABLE (CWE-561): [#def164]
dnsmasq-2.86test7/src/util.c:239: unreachable: This code cannot be reached: "if (ret = whine_malloc(strl...".
 #  237|   #endif
 #  238|
 #  239|->   if ((ret = whine_malloc(strlen(in)+1)))
 #  240|       strcpy(ret, in);
 #  241|     else if (nomem)

Error: CLANG_WARNING: [#def165]
dnsmasq-2.86test7/src/util.c:531:2: warning[deadcode.DeadStores]: Value stored to 'p' is never read
 #  529|   	p += sprintf(&buf[p], "%um", x);
 #  530|         if ((x = t%60))
 #  531|-> 	p += sprintf(&buf[p], "%us", x);
 #  532|       }
 #  533|   }

Error: CPPCHECK_WARNING (CWE-456): [#def166]
dnsmasq-2.86test7/src/util.c:577: error[uninitvar]: Uninitialized variable: sav
 #  575|   		  for (j = 0; j < bytes; j++)
 #  576|   		    {
 #  577|-> 		      char sav = sav;
 #  578|   		      if (j < bytes - 1)
 #  579|   			{

Error: CLANG_WARNING: [#def167]
dnsmasq-2.86test7/src/util.c:577:9: warning[core.uninitialized.Assign]: Assigned value is garbage or undefined
 #  575|   		  for (j = 0; j < bytes; j++)
 #  576|   		    {
 #  577|-> 		      char sav = sav;
 #  578|   		      if (j < bytes - 1)
 #  579|   			{

Error: MISSING_RESTORE (CWE-573): [#def168]
dnsmasq-2.86test7/src/util.c:580: save: Saving non-local "in[(j + 1) * 2]" in local "sav".
dnsmasq-2.86test7/src/util.c:581: modify: Modifying non-local "in[(j + 1) * 2]".
dnsmasq-2.86test7/src/util.c:586: end_of_scope: Value of non-local "in[(j + 1) * 2]" that was saved in "sav" is not restored as it was along other paths.
dnsmasq-2.86test7/src/util.c:592: restore_example: The original value of non-local "in[(j + 1) * 2]" was restored here.
 #  584|   			 is illegal. */
 #  585|   		      if (strchr(&in[j*2], '*'))
 #  586|-> 			return -1;
 #  587|   		      out[i] = strtol(&in[j*2], NULL, 16);
 #  588|   		      mask = mask << 1;
---
 src/util.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/src/util.c b/src/util.c
index 1425764..8e69d55 100644
--- a/src/util.c
+++ b/src/util.c
@@ -208,6 +208,8 @@ char *canonicalise(char *in, int *nomem)
   /* older libidn2 strips underscores, so don't do IDN processing
      if the name has an underscore (check_name() returned 2) */
   if (rc != 2)
+#else
+  (void)rc;
 #endif
 #if defined(HAVE_IDN) || defined(HAVE_LIBIDN2)
     {
@@ -235,11 +237,14 @@ char *canonicalise(char *in, int *nomem)
       return ret;
     }
 #endif
-  
+
+#if !defined(HAVE_LIBIDN2) || (defined(HAVE_LIBIDN2) && (!defined(IDN2_VERSION_NUMBER) || IDN2_VERSION_NUMBER < 0x02000003))
+  /* If recent libidn2 is used, it cannot reach this code. */
   if ((ret = whine_malloc(strlen(in)+1)))
     strcpy(ret, in);
   else if (nomem)
-    *nomem = 1;    
+    *nomem = 1;
+#endif
 
   return ret;
 }
@@ -528,7 +533,7 @@ void prettyprint_time(char *buf, unsigned int t)
       if ((x = (t/60)%60))
 	p += sprintf(&buf[p], "%um", x);
       if ((x = t%60))
-	p += sprintf(&buf[p], "%us", x);
+	sprintf(&buf[p], "%us", x);
     }
 }
 
@@ -574,7 +579,7 @@ int parse_hex(char *in, unsigned char *out, int maxlen,
 		  int j, bytes = (1 + (r - in))/2;
 		  for (j = 0; j < bytes; j++)
 		    { 
-		      char sav = sav;
+		      char sav;
 		      if (j < bytes - 1)
 			{
 			  sav = in[(j+1)*2];
-- 
2.31.1