dhcpcd-discuss

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

Shahid Mahmood

Tue Sep 11 13:12:45 2018

Here is the complete function, annotated with [Coverity]
(it is basically unmodified dhcpcd code from rel 7.08):
----------------------------
static size_t
 64duid_machineuuid(char *uuid, size_t uuid_len)
 65{
 66        int r;
 67        size_t len = uuid_len;
 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.
 97        if (r == -1)

[Coverity] CID 939434 (#1 of 1): Logically dead code (DEADCODE)
dead_error_line: Execution cannot reach this statement: return 0U;.
 98                return 0;
 99        return len;
100}

------

On Mon, Sep 10, 2018 at 5:07 PM Roy Marples <roy@xxxxxxxxxxxx> wrote:

> 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
>

Follow-Ups:
Re: dhcpcd-7.0.8 Coverity detected defect - Logically dead code (DEADCODE)Roy Marples
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
Re: dhcpcd-7.0.8 Coverity detected defect - Logically dead code (DEADCODE)Roy Marples
Archive administrator: postmaster@marples.name