Menu

#119 Useless check for dirent.d_name==NULL

all
open-fixed
nobody
None
5
2018-02-04
2017-10-14
No

Building xmlstar-1.6.1 on OS X 10.13:

src/xml_ls.c:149:15: warning: comparison of array 'd->d_name' equal to a null pointer is always false [-Wtautological-pointer-compare]
      if ((d->d_name == NULL) || !strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
           ~~~^~~~~~    ~~~~

The dirent.d_name member is a fixed-length char array, not a pointer to a separately allocated array. Given d itself wasn't null, d->d_name would be some valid string. But if you want to be sure (or at least short-circuit having to do strcmp when the string that exists is null), you could instead check d_namlen, as in the attached patch.

1 Attachments

Discussion

  • Noam Postavsky

    Noam Postavsky - 2018-02-04

    Thanks!

    you could instead check d_namlen, as in the attached patch.

    Unfortunately, that's not portable:

    src/xml_ls.c: In function xml_print_dir:
    src/xml_ls.c:149:13: error: struct dirent has no member named d_namlen; did you mean d_name?
           if ((d->d_namlen != 0) || !strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
    

    So I just removed the check instead.

     
  • Noam Postavsky

    Noam Postavsky - 2018-02-04
    • status: open --> open-fixed
     

Log in to post a comment.

MongoDB Logo MongoDB