dhcpcd-discuss

Re: dhcpcd-7.0.8 Coverity detected defect - Logically dead code (DEADCODE)

Roy Marples

Mon Sep 10 21:06:40 2018

On 10/09/2018 18:54, Stuart Henderson wrote:
On 2018/09/10 13:45, Shahid Mahmood wrote:
Hi Roy,
Our coverity analysis reported the following defect. Just so you know:

  63static size_t
  64duid_machineuuid(char *uuid, size_t uuid_len)
  65{
  66        int r;
  67        size_t len = uuid_len;
....

It makes more sense if you fill in the "..." :

Yes, the context here is very important.


  68
  69 #if defined(HW_UUID) /* OpenBSD */
  70         int mib[] = { CTL_HW, HW_UUID };
  71
  72         r = sysctl(mib, sizeof(mib)/sizeof(mib[0]), uuid, &len, NULL, 0);
  73 #elif defined(KERN_HOSTUUID) /* FreeBSD */
  74         int mib[] = { CTL_KERN, KERN_HOSTUUID };
  75
  76         r = sysctl(mib, sizeof(mib)/sizeof(mib[0]), uuid, &len, NULL, 0);
  77 #elif defined(__NetBSD__)
  78         r = sysctlbyname("machdep.dmi.system-uuid", uuid, &len, NULL, 0);
  79 #elif defined(__linux__)
  80         FILE *fp;
  81
  82         fp = fopen("/sys/class/dmi/id/product_uuid", "r");
  83         if (fp == NULL)
  84                 return 0;
85        if (fgets(uuid, (int)uuid_len, fp) == NULL) {
  86                fclose(fp);
  87                return 0;
  88        }
  89        len = strlen(uuid) + 1;
  90        fclose(fp);
[Coverity]assignment: Assigning: r = 0.
  91        r = 0;
  92#else
  93        r = -1;
  94        errno = ENOSYS;
  95#endif
  96
[Coverity] const: At condition r == -1, the value of r must be equal to 0. [Coverity] dead_error_condition: The condition r == -1 cannot be true.

there are other OS than Linux ..

Indeed there are.
No matter how impossible it is to silence warnings compiling dhcpcd on Linux (and there are a LOT which pains me), we should still silence where we can. It's not the first time coverity has detected false positives before either.

The attached patch should solve it - I can't test it as coverity does not work on my one Linux VM.
Let me know if it works for you!


  97        if (r == -1)
[Coverity]  Logically dead code (DEADCODE)dead_error_line: Execution cannot reach this
statement: return 0U;.
  98                return 0;
  99        return len;
100}

Regards,
-shahid

Roy
diff --git a/src/duid.c b/src/duid.c
index cc53f9bd..45223435 100644
--- a/src/duid.c
+++ b/src/duid.c
@@ -94,6 +94,7 @@ duid_machineuuid(char *uuid, size_t uuid_len)
 	errno = ENOSYS;
 #endif
 
+	/* coverity[dead_error_condition] False positive on Linux */
 	if (r == -1)
 		return 0;
 	return len;

Follow-Ups:
Re: dhcpcd-7.0.8 Coverity detected defect - Logically dead code (DEADCODE)Shahid Mahmood
References:
dhcpcd-7.0.8 Coverity detected defect - Logically dead code (DEADCODE)Shahid Mahmood
Re: dhcpcd-7.0.8 Coverity detected defect - Logically dead code (DEADCODE)Stuart Henderson
Archive administrator: postmaster@marples.name