Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: Fix musl warnings #5431

Merged
merged 2 commits into from
Oct 23, 2022
Merged

build: Fix musl warnings #5431

merged 2 commits into from
Oct 23, 2022

Conversation

reinerh
Copy link
Collaborator

@reinerh reinerh commented Oct 23, 2022

Fixes two warnings when building firejail with musl libc.

Should fix #5421.

Copy link
Collaborator

@glitsj16 glitsj16 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks okay. Alas I've got no way to test this properly.

@rusty-snake
Copy link
Collaborator

The LINE_MAX warning/error is gone. However I still get the libtrace(log) redefinition of '...' warnings/errors.

For testing I just use an podman container.

$ podman run --rm -it alpine:latest
# apk update
# apk add git build-base gawk linux-headers
# git clone --depth=1 https://github.com/netblue30/firejail.git
# cd firejail
# git fetch origin pull/5431/head:pr-5431
# git switch pr-5431
# ./configure --enable-fatal-warnings [--enable-analyzer]
# make -j4
# gcc --version
gcc (Alpine 11.2.1_git20220219) 11.2.1 20220219
libtrace(log) redefinition of '...'
In file included from /usr/include/fortify/stdio.h:22,
                 from libtrace.c:22:
libtrace.c:344:7: error: redefinition of 'fopen'
  344 | FILE *fopen64(const char *pathname, const char *mode) {
      |       ^~~~~~~
/usr/include/stdio.h:70:7: note: previous definition of 'fopen' with type 'FILE *(const char * restrict,  const char * restrict)'
   70 | FILE *fopen(const char *__restrict, const char *__restrict);
      |       ^~~~~
libtrace.c:368:7: error: redefinition of 'freopen'
  368 | FILE *freopen64(const char *pathname, const char *mode, FILE *stream) {
      |       ^~~~~~~~~
/usr/include/stdio.h:71:7: note: previous definition of 'freopen' with type 'FILE *(const char * restrict,  const char * restrict,  FILE * restrict)'
   71 | FILE *freopen(const char *__restrict, const char *__restrict, FILE *__restrict);
      |       ^~~~~~~
In file included from libtrace.c:32:
libtrace.c:448:5: error: redefinition of 'stat'
  448 | int stat64(const char *pathname, struct stat64 *statbuf) {
      |     ^~~~~~
/usr/include/sys/stat.h:73:5: note: previous definition of 'stat' with type 'int(const char * restrict,  struct stat * restrict)'
   73 | int stat(const char *__restrict, struct stat *__restrict);
      |     ^~~~
libtrace.c:471:5: error: redefinition of 'lstat'
  471 | int lstat64(const char *pathname, struct stat64 *statbuf) {
      |     ^~~~~~~
/usr/include/sys/stat.h:75:5: note: previous definition of 'lstat' with type 'int(const char * restrict,  struct stat * restrict)'
   75 | int lstat(const char *__restrict, struct stat *__restrict);
      |     ^~~~~
In file included from /usr/include/fortify/stdio.h:22,
                 from libtracelog.c:21:
libtracelog.c:408:7: error: redefinition of 'fopen'
  408 | FILE *fopen64(const char *pathname, const char *mode) {
      |       ^~~~~~~
/usr/include/stdio.h:70:7: note: previous definition of 'fopen' with type 'FILE *(const char * restrict,  const char * restrict)'
   70 | FILE *fopen(const char *__restrict, const char *__restrict);
      |       ^~~~~
libtracelog.c:444:7: error: redefinition of 'freopen'
  444 | FILE *freopen64(const char *pathname, const char *mode, FILE *stream) {
      |       ^~~~~~~~~
/usr/include/stdio.h:71:7: note: previous definition of 'freopen' with type 'FILE *(const char * restrict,  const char * restrict,  FILE * restrict)'
   71 | FILE *freopen(const char *__restrict, const char *__restrict, FILE *__restrict);
      |       ^~~~~~~
In file included from libtracelog.c:31:
libtracelog.c:566:5: error: redefinition of 'stat'
  566 | int stat64(const char *pathname, struct stat64 *buf) {
      |     ^~~~~~
/usr/include/sys/stat.h:73:5: note: previous definition of 'stat' with type 'int(const char * restrict,  struct stat * restrict)'
   73 | int stat(const char *__restrict, struct stat *__restrict);
      |     ^~~~
libtracelog.c:600:5: error: redefinition of 'lstat'
  600 | int lstat64(const char *pathname, struct stat64 *buf) {
      |     ^~~~~~~
/usr/include/sys/stat.h:75:5: note: previous definition of 'lstat' with type 'int(const char * restrict,  struct stat * restrict)'
   75 | int lstat(const char *__restrict, struct stat *__restrict);
      |     ^~~~~

Running with --enable-analyzer gives me also this list. IDK if they are false-positive.

double-'free' of 'rdest' [CWE-415] [-Werror=analyzer-double-free]
main.c: In function 'duplicate_dir':
main.c:369:9: error: double-'free' of 'rdest' [CWE-415] [-Werror=analyzer-double-free]
  369 |         free(rdest);
      |         ^~~~~~~~~~~
  'duplicate_dir': events 1-2
    |
    |  355 | static void duplicate_dir(const char *src, const char *dest, struct stat *s) {
    |      |             ^~~~~~~~~~~~~
    |      |             |
    |      |             (1) entry to 'duplicate_dir'
    |  356 |         (void) s;
    |  357 |         char *rsrc = check(src);
    |      |                      ~~~~~~~~~~
    |      |                      |
    |      |                      (2) calling 'check' from 'duplicate_dir'
    |
    +--> 'check': events 3-7
           |
           |  321 | static char *check(const char *src) {
           |      |              ^~~~~
           |      |              |
           |      |              (3) entry to 'check'
           |......
           |  324 |         if (!rsrc || stat(rsrc, &s) == -1)
           |      |            ~  
           |      |            |
           |      |            (4) following 'false' branch...
           |......
           |  330 |         uid_t user = getuid();
           |      |         ~~~~~ 
           |      |         |
           |      |         (5) ...to here
           |......
           |  340 |                 if (s.st_uid != user)
           |      |                    ~
           |      |                    |
           |      |                    (6) following 'false' branch...
           |......
           |  345 |         if (S_ISDIR(s.st_mode) || S_ISREG(s.st_mode) || S_ISLNK(s.st_mode))
           |      |         ~~    
           |      |         |
           |      |         (7) ...to here
           |
    <------+
    |
  'duplicate_dir': events 8-9
    |
    |  357 |         char *rsrc = check(src);
    |      |                      ^~~~~~~~~~
    |      |                      |
    |      |                      (8) returning to 'duplicate_dir' from 'check'
    |  358 |         char *rdest = check(dest);
    |      |                       ~~~~~~~~~~~
    |      |                       |
    |      |                       (9) calling 'check' from 'duplicate_dir'
    |
    +--> 'check': events 10-14
           |
           |  321 | static char *check(const char *src) {
           |      |              ^~~~~
           |      |              |
           |      |              (10) entry to 'check'
           |......
           |  324 |         if (!rsrc || stat(rsrc, &s) == -1)
           |      |            ~  
           |      |            |
           |      |            (11) following 'false' branch...
           |......
           |  330 |         uid_t user = getuid();
           |      |         ~~~~~ 
           |      |         |
           |      |         (12) ...to here
           |......
           |  340 |                 if (s.st_uid != user)
           |      |                    ~
           |      |                    |
           |      |                    (13) following 'false' branch...
           |......
           |  345 |         if (S_ISDIR(s.st_mode) || S_ISREG(s.st_mode) || S_ISLNK(s.st_mode))
           |      |         ~~    
           |      |         |
           |      |         (14) ...to here
           |
    <------+
    |
  'duplicate_dir': events 15-19
    |
    |  358 |         char *rdest = check(dest);
    |      |                       ^~~~~~~~~~~
    |      |                       |
    |      |                       (15) returning to 'duplicate_dir' from 'check'
    |......
    |  363 |         if(nftw(rsrc, fs_copydir, 1, FTW_PHYS) != 0) {
    |      |           ~            
    |      |           |
    |      |           (16) following 'false' branch...
    |......
    |  368 |         free(rsrc);
    |      |         ~~~~~~~~~~     
    |      |         |
    |      |         (17) ...to here
    |      |         (18) first 'free' here
    |  369 |         free(rdest);
    |      |         ~~~~~~~~~~~    
    |      |         |
    |      |         (19) second 'free' here; first 'free' was at (18)
    |
double-'free' of 'rdest' [CWE-415] [-Werror=analyzer-double-free]
main.c: In function 'duplicate_file':
main.c:392:9: error: double-'free' of 'rdest' [CWE-415] [-Werror=analyzer-double-free]
  392 |         free(rdest);
      |         ^~~~~~~~~~~
  'duplicate_file': events 1-2
    |
    |  373 | static void duplicate_file(const char *src, const char *dest, struct stat *s) {
    |      |             ^~~~~~~~~~~~~~
    |      |             |
    |      |             (1) entry to 'duplicate_file'
    |  374 |         char *rsrc = check(src);
    |      |                      ~~~~~~~~~~
    |      |                      |
    |      |                      (2) calling 'check' from 'duplicate_file'
    |
    +--> 'check': events 3-7
           |
           |  321 | static char *check(const char *src) {
           |      |              ^~~~~
           |      |              |
           |      |              (3) entry to 'check'
           |......
           |  324 |         if (!rsrc || stat(rsrc, &s) == -1)
           |      |            ~  
           |      |            |
           |      |            (4) following 'false' branch...
           |......
           |  330 |         uid_t user = getuid();
           |      |         ~~~~~ 
           |      |         |
           |      |         (5) ...to here
           |......
           |  340 |                 if (s.st_uid != user)
           |      |                    ~
           |      |                    |
           |      |                    (6) following 'false' branch...
           |......
           |  345 |         if (S_ISDIR(s.st_mode) || S_ISREG(s.st_mode) || S_ISLNK(s.st_mode))
           |      |         ~~    
           |      |         |
           |      |         (7) ...to here
           |
    <------+
    |
  'duplicate_file': events 8-9
    |
    |  374 |         char *rsrc = check(src);
    |      |                      ^~~~~~~~~~
    |      |                      |
    |      |                      (8) returning to 'duplicate_file' from 'check'
    |  375 |         char *rdest = check(dest);
    |      |                       ~~~~~~~~~~~
    |      |                       |
    |      |                       (9) calling 'check' from 'duplicate_file'
    |
    +--> 'check': events 10-14
           |
           |  321 | static char *check(const char *src) {
           |      |              ^~~~~
           |      |              |
           |      |              (10) entry to 'check'
           |......
           |  324 |         if (!rsrc || stat(rsrc, &s) == -1)
           |      |            ~  
           |      |            |
           |      |            (11) following 'false' branch...
           |......
           |  330 |         uid_t user = getuid();
           |      |         ~~~~~ 
           |      |         |
           |      |         (12) ...to here
           |......
           |  340 |                 if (s.st_uid != user)
           |      |                    ~
           |      |                    |
           |      |                    (13) following 'false' branch...
           |......
           |  345 |         if (S_ISDIR(s.st_mode) || S_ISREG(s.st_mode) || S_ISLNK(s.st_mode))
           |      |         ~~    
           |      |         |
           |      |         (14) ...to here
           |
    <------+
    |
  'duplicate_file': events 15-16
    |
    |  375 |         char *rdest = check(dest);
    |      |                       ^~~~~~~~~~~
    |      |                       |
    |      |                       (15) returning to 'duplicate_file' from 'check'
    |......
    |  384 |         if (asprintf(&name, "%s/%s", rdest, ptr) == -1)
    |      |            ~           
    |      |            |
    |      |            (16) following 'false' branch...
    |
  'duplicate_file': event 17
    |
    |../include/common.h:39:28:
    |   39 | #define errExit(msg)    do { char msgout[500]; snprintf(msgout, 500, "Error %s: %s:%d %s", msg, __FILE__, __LINE__, __FUNCTION__); perror(msgout); exit(1);} while (0)
    |      |                            ^
    |      |                            |
    |      |                            (17) ...to here
main.c:385:17: note: in expansion of macro 'errExit'
    |  385 |                 errExit("asprintf");
    |      |                 ^~~~~~~
    |
  'duplicate_file': event 18
    |
    |  388 |         copy_file(rsrc, name, mode, uid, gid);
    |      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |         |
    |      |         (18) calling 'copy_file' from 'duplicate_file'
    |
    +--> 'copy_file': event 19
           |
           |  104 | static void copy_file(const char *srcname, const char *destname, mode_t mode, uid_t uid, gid_t gid) {
           |      |             ^~~~~~~~~
           |      |             |
           |      |             (19) entry to 'copy_file'
           |
         'copy_file': event 20
           |
           |  105 |         assert(srcname);
           |      |         ^~~~~~
           |      |         |
           |      |         (20) following 'true' branch (when 'srcname' is non-NULL)...
           |
         'copy_file': event 21
           |
           |  106 |         assert(destname);
           |      |         ^~~~~~
           |      |         |
           |      |         (21) ...to here
           |
         'copy_file': event 22
           |
           |  106 |         assert(destname);
           |      |         ^~~~~~
           |      |         |
           |      |         (22) following 'true' branch (when 'destname' is non-NULL)...
           |
         'copy_file': event 23
           |
           |  107 |         mode &= 07777;
           |      |         ^~~~
           |      |         |
           |      |         (23) ...to here
           |
    <------+
    |
  'duplicate_file': events 24-26
    |
    |  388 |         copy_file(rsrc, name, mode, uid, gid);
    |      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    |      |         |
    |      |         (24) returning to 'duplicate_file' from 'copy_file'
    |......
    |  391 |         free(rsrc);
    |      |         ~~~~~~~~~~
    |      |         |
    |      |         (25) first 'free' here
    |  392 |         free(rdest);
    |      |         ~~~~~~~~~~~
    |      |         |
    |      |         (26) second 'free' here; first 'free' was at (25)
    |
double-'free' of 'rdest' [CWE-415] [-Werror=analyzer-double-free]
main.c: In function 'duplicate_link':
main.c:416:9: error: double-'free' of 'rdest' [CWE-415] [-Werror=analyzer-double-free]
  416 |         free(rdest);
      |         ^~~~~~~~~~~
  'duplicate_link': events 1-2
    |
    |  396 | static void duplicate_link(const char *src, const char *dest, struct stat *s) {
    |      |             ^~~~~~~~~~~~~~
    |      |             |
    |      |             (1) entry to 'duplicate_link'
    |  397 |         char *rsrc = check(src);                  // we drop the result and use the original name
    |      |                      ~~~~~~~~~~
    |      |                      |
    |      |                      (2) calling 'check' from 'duplicate_link'
    |
    +--> 'check': events 3-7
           |
           |  321 | static char *check(const char *src) {
           |      |              ^~~~~
           |      |              |
           |      |              (3) entry to 'check'
           |......
           |  324 |         if (!rsrc || stat(rsrc, &s) == -1)
           |      |            ~  
           |      |            |
           |      |            (4) following 'false' branch...
           |......
           |  330 |         uid_t user = getuid();
           |      |         ~~~~~ 
           |      |         |
           |      |         (5) ...to here
           |......
           |  340 |                 if (s.st_uid != user)
           |      |                    ~
           |      |                    |
           |      |                    (6) following 'false' branch...
           |......
           |  345 |         if (S_ISDIR(s.st_mode) || S_ISREG(s.st_mode) || S_ISLNK(s.st_mode))
           |      |         ~~    
           |      |         |
           |      |         (7) ...to here
           |
    <------+
    |
  'duplicate_link': events 8-9
    |
    |  397 |         char *rsrc = check(src);                  // we drop the result and use the original name
    |      |                      ^~~~~~~~~~
    |      |                      |
    |      |                      (8) returning to 'duplicate_link' from 'check'
    |  398 |         char *rdest = check(dest);
    |      |                       ~~~~~~~~~~~
    |      |                       |
    |      |                       (9) calling 'check' from 'duplicate_link'
    |
    +--> 'check': events 10-14
           |
           |  321 | static char *check(const char *src) {
           |      |              ^~~~~
           |      |              |
           |      |              (10) entry to 'check'
           |......
           |  324 |         if (!rsrc || stat(rsrc, &s) == -1)
           |      |            ~  
           |      |            |
           |      |            (11) following 'false' branch...
           |......
           |  330 |         uid_t user = getuid();
           |      |         ~~~~~ 
           |      |         |
           |      |         (12) ...to here
           |......
           |  340 |                 if (s.st_uid != user)
           |      |                    ~
           |      |                    |
           |      |                    (13) following 'false' branch...
           |......
           |  345 |         if (S_ISDIR(s.st_mode) || S_ISREG(s.st_mode) || S_ISLNK(s.st_mode))
           |      |         ~~    
           |      |         |
           |      |         (14) ...to here
           |
    <------+
    |
  'duplicate_link': events 15-16
    |
    |  398 |         char *rdest = check(dest);
    |      |                       ^~~~~~~~~~~
    |      |                       |
    |      |                       (15) returning to 'duplicate_link' from 'check'
    |......
    |  408 |         if (asprintf(&name, "%s/%s", rdest, ptr) == -1)
    |      |            ~           
    |      |            |
    |      |            (16) following 'false' branch...
    |
  'duplicate_link': event 17
    |
    |../include/common.h:39:28:
    |   39 | #define errExit(msg)    do { char msgout[500]; snprintf(msgout, 500, "Error %s: %s:%d %s", msg, __FILE__, __LINE__, __FUNCTION__); perror(msgout); exit(1);} while (0)
    |      |                            ^
    |      |                            |
    |      |                            (17) ...to here
main.c:409:17: note: in expansion of macro 'errExit'
    |  409 |                 errExit("asprintf");
    |      |                 ^~~~~~~
    |
  'duplicate_link': events 18-19
    |
    |  415 |         free(rsrc);
    |      |         ^~~~~~~~~~
    |      |         |
    |      |         (18) first 'free' here
    |  416 |         free(rdest);
    |      |         ~~~~~~~~~~~
    |      |         |
    |      |         (19) second 'free' here; first 'free' was at (18)
    |
use of possibly-NULL 'strdup(fname)' where non-null expected [CWE-690] [-Werror=analyzer-possible-null-argument]
filedb.c: In function 'filedb_add.part.0':
filedb.c:69:22: error: use of possibly-NULL 'strdup(fname)' where non-null expected [CWE-690] [-Werror=analyzer-possible-null-argument]
   69 |         entry->len = strlen(entry->fname);
      |                      ^~~~~~~~~~~~~~~~~~~~
  'filedb_load_whitelist': event 1
    |
    |   88 | FileDB *filedb_load_whitelist(FileDB *head, const char *fname, const char *prefix) {
    |      |         ^~~~~~~~~~~~~~~~~~~~~
    |      |         |
    |      |         (1) entry to 'filedb_load_whitelist'
    |
  'filedb_load_whitelist': event 2
    |
    |   89 |         assert(fname);
    |      |         ^~~~~~
    |      |         |
    |      |         (2) following 'true' branch (when 'fname' is non-NULL)...
    |
  'filedb_load_whitelist': event 3
    |
    |   90 |         assert(prefix);
    |      |         ^~~~~~
    |      |         |
    |      |         (3) ...to here
    |
  'filedb_load_whitelist': event 4
    |
    |   90 |         assert(prefix);
    |      |         ^~~~~~
    |      |         |
    |      |         (4) following 'true' branch (when 'prefix' is non-NULL)...
    |
  'filedb_load_whitelist': events 5-6
    |
    |   91 |         int len = strlen(prefix);
    |      |         ^~~
    |      |         |
    |      |         (5) ...to here
    |   92 |         char *f;
    |   93 |         if (asprintf(&f, "%s/%s", SYSCONFDIR, fname) == -1)
    |      |            ~
    |      |            |
    |      |            (6) following 'false' branch...
    |
  'filedb_load_whitelist': event 7
    |
    |../include/common.h:39:28:
    |   39 | #define errExit(msg)    do { char msgout[500]; snprintf(msgout, 500, "Error %s: %s:%d %s", msg, __FILE__, __LINE__, __FUNCTION__); perror(msgout); exit(1);} while (0)
    |      |                            ^
    |      |                            |
    |      |                            (7) ...to here
filedb.c:94:17: note: in expansion of macro 'errExit'
    |   94 |                 errExit("asprintf");
    |      |                 ^~~~~~~
    |
  'filedb_load_whitelist': event 8
    |
    |   96 |         if (!fp) {
    |      |            ^
    |      |            |
    |      |            (8) following 'false' branch (when 'fp' is non-NULL)...
    |
  'filedb_load_whitelist': event 9
    |
    |cc1:
    | (9): ...to here
    |
  'filedb_load_whitelist': events 10-12
    |
    |  103 |         while (fgets(buf, MAX_BUF, fp)) {
    |      |                ^~~~~
    |      |                |
    |      |                (10) following 'true' branch...
    |  104 |                 if (strncmp(buf, prefix, len) != 0)
    |      |                 ~~
    |      |                 |
    |      |                 (11) ...to here
    |......
    |  114 |                 head = filedb_add(head, fn);
    |      |                        ~~~~~~~~~~~~~~~~~~~~
    |      |                        |
    |      |                        (12) calling 'filedb_add' from 'filedb_load_whitelist'
    |
    +--> 'filedb_add': event 13
           |
           |   54 | FileDB *filedb_add(FileDB *head, const char *fname) {
           |      |         ^~~~~~~~~~
           |      |         |
           |      |         (13) entry to 'filedb_add'
           |
         'filedb_add': event 14
           |
           |   55 |         assert(fname);
           |      |         ^~~~~~
           |      |         |
           |      |         (14) following 'true' branch (when 'fname' is non-NULL)...
           |
         'filedb_add': events 15-16
           |
           |   58 |         if (filedb_find(head, fname))
           |      |         ^~  ~~~~~~~~~~~~~~~~~~~~~~~~
           |      |         |   |
           |      |         |   (16) calling 'filedb_find' from 'filedb_add'
           |      |         (15) ...to here
           |
           +--> 'filedb_find': event 17
                  |
                  |   24 | FileDB *filedb_find(FileDB *head, const char *fname) {
                  |      |         ^~~~~~~~~~~
                  |      |         |
                  |      |         (17) entry to 'filedb_find'
                  |
                'filedb_find': event 18
                  |
                  |   25 |         assert(fname);
                  |      |         ^~~~~~
                  |      |         |
                  |      |         (18) following 'true' branch (when 'fname' is non-NULL)...
                  |
                'filedb_find': event 19
                  |
                  |cc1:
                  | (19): ...to here
                  |
                'filedb_find': event 20
                  |
                  |cc1:
                  | (20): calling 'filedb_find.part.0' from 'filedb_find'
                  |
                  +--> 'filedb_find.part.0': events 21-27
                         |
                         |   24 | FileDB *filedb_find(FileDB *head, const char *fname) {
                         |      |         ^~~~~~~~~~~
                         |      |         |
                         |      |         (21) entry to 'filedb_find.part.0'
                         |......
                         |   29 |         while (ptr) {
                         |      |                ~~~
                         |      |                |
                         |      |                (22) following 'true' branch (when 'ptr' is non-NULL)...
                         |      |                (24) following 'true' branch (when 'ptr' is non-NULL)...
                         |......
                         |   32 |                 if (fnmatch(ptr->fname, fname, FNM_PATHNAME) == 0) {
                         |      |                 ~~
                         |      |                 |
                         |      |                 (23) ...to here
                         |      |                 (25) ...to here
                         |......
                         |   48 |         if (found)
                         |      |            ~
                         |      |            |
                         |      |            (26) following 'true' branch (when 'found != 0')...
                         |   49 |                 return ptr;
                         |      |                 ~~~~~~
                         |      |                 |
                         |      |                 (27) ...to here
                         |
                  <------+
                  |
                'filedb_find': event 28
                  |
                  |cc1:
                  | (28): returning to 'filedb_find' from 'filedb_find.part.0'
                  |
           <------+
           |
         'filedb_add': event 29
           |
           |   58 |         if (filedb_find(head, fname))
           |      |             ^~~~~~~~~~~~~~~~~~~~~~~~
           |      |             |
           |      |             (29) returning to 'filedb_add' from 'filedb_find'
           |
         'filedb_add': event 30
           |
           |cc1:
           | (30): calling 'filedb_add.part.0' from 'filedb_add'
           |
           +--> 'filedb_add.part.0': events 31-32
                  |
                  |   54 | FileDB *filedb_add(FileDB *head, const char *fname) {
                  |      |         ^~~~~~~~~~
                  |      |         |
                  |      |         (31) entry to 'filedb_add.part.0'
                  |......
                  |   63 |         if (!entry)
                  |      |            ~
                  |      |            |
                  |      |            (32) following 'false' branch (when 'entry' is non-NULL)...
                  |
                'filedb_add.part.0': event 33
                  |
                  |../include/common.h:39:28:
                  |   39 | #define errExit(msg)    do { char msgout[500]; snprintf(msgout, 500, "Error %s: %s:%d %s", msg, __FILE__, __LINE__, __FUNCTION__); perror(msgout); exit(1);} while (0)
                  |      |                            ^
                  |      |                            |
                  |      |                            (33) ...to here
filedb.c:64:17: note: in expansion of macro 'errExit'
                  |   64 |                 errExit("malloc");
                  |      |                 ^~~~~~~
                  |
                'filedb_add.part.0': events 34-35
                  |
                  |/usr/include/fortify/string.h:70:12:
                  |   70 |         if (__n > __b)
                  |      |            ^
                  |      |            |
                  |      |            (34) following 'false' branch (when '__b > 23')...
                  |   71 |                 __builtin_trap();
                  |   72 |         return __builtin_memset(__d, __c, __n);
                  |      |         ~~~~~~
                  |      |         |
                  |      |         (35) ...to here
                  |
                'filedb_add.part.0': events 36-38
                  |
                  |filedb.c:66:24:
                  |   66 |         entry->fname = strdup(fname);
                  |      |                        ^~~~~~~~~~~~~
                  |      |                        |
                  |      |                        (36) this call could return NULL
                  |   67 |         if (!entry->fname)
                  |      |            ~            
                  |      |            |
                  |      |            (37) assuming 'strdup(fname)' is non-NULL
                  |      |            (38) following 'false' branch...
                  |
                'filedb_add.part.0': event 39
                  |
                  |../include/common.h:39:28:
                  |   39 | #define errExit(msg)    do { char msgout[500]; snprintf(msgout, 500, "Error %s: %s:%d %s", msg, __FILE__, __LINE__, __FUNCTION__); perror(msgout); exit(1);} while (0)
                  |      |                            ^
                  |      |                            |
                  |      |                            (39) ...to here
filedb.c:68:17: note: in expansion of macro 'errExit'
                  |   68 |                 errExit("strdup");
                  |      |                 ^~~~~~~
                  |
           <------+
           |
         'filedb_add': event 40
           |
           |cc1:
           | (40): returning to 'filedb_add' from 'filedb_add.part.0'
           |
    <------+
    |
  'filedb_load_whitelist': events 41-48
    |
    |  103 |         while (fgets(buf, MAX_BUF, fp)) {
    |      |                ~~~~~    
    |      |                |
    |      |                (42) following 'true' branch...
    |  104 |                 if (strncmp(buf, prefix, len) != 0)
    |      |                 ~~ ~    
    |      |                 |  |
    |      |                 |  (44) following 'false' branch...
    |      |                 (43) ...to here
    |......
    |  107 |                 char *fn = buf + len;
    |      |                 ~~~~    
    |      |                 |
    |      |                 (45) ...to here
    |  108 |                 char *ptr = strchr(buf, '\n');
    |  109 |                 if (!ptr)
    |      |                    ~    
    |      |                    |
    |      |                    (46) following 'false' branch (when 'ptr' is non-NULL)...
    |  110 |                         continue;
    |  111 |                 *ptr = '\0';
    |      |                 ~       
    |      |                 |
    |      |                 (47) ...to here
    |......
    |  114 |                 head = filedb_add(head, fn);
    |      |                        ^~~~~~~~~~~~~~~~~~~~
    |      |                        |
    |      |                        (41) returning to 'filedb_load_whitelist' from 'filedb_add'
    |      |                        (48) calling 'filedb_add' from 'filedb_load_whitelist'
    |
    +--> 'filedb_add': event 49
           |
           |   54 | FileDB *filedb_add(FileDB *head, const char *fname) {
           |      |         ^~~~~~~~~~
           |      |         |
           |      |         (49) entry to 'filedb_add'
           |
         'filedb_add': event 50
           |
           |   55 |         assert(fname);
           |      |         ^~~~~~
           |      |         |
           |      |         (50) following 'true' branch (when 'fname' is non-NULL)...
           |
         'filedb_add': events 51-52
           |
           |   58 |         if (filedb_find(head, fname))
           |      |         ^~  ~~~~~~~~~~~~~~~~~~~~~~~~
           |      |         |   |
           |      |         |   (52) calling 'filedb_find' from 'filedb_add'
           |      |         (51) ...to here
           |
           +--> 'filedb_find': event 53
                  |
                  |   24 | FileDB *filedb_find(FileDB *head, const char *fname) {
                  |      |         ^~~~~~~~~~~
                  |      |         |
                  |      |         (53) entry to 'filedb_find'
                  |
                'filedb_find': event 54
                  |
                  |   25 |         assert(fname);
                  |      |         ^~~~~~
                  |      |         |
                  |      |         (54) following 'true' branch (when 'fname' is non-NULL)...
                  |
                'filedb_find': event 55
                  |
                  |cc1:
                  | (55): ...to here
                  |
                'filedb_find': event 56
                  |
                  |cc1:
                  | (56): calling 'filedb_find.part.0' from 'filedb_find'
                  |
                  +--> 'filedb_find.part.0': events 57-62
                         |
                         |   24 | FileDB *filedb_find(FileDB *head, const char *fname) {
                         |      |         ^~~~~~~~~~~
                         |      |         |
                         |      |         (57) entry to 'filedb_find.part.0'
                         |......
                         |   29 |         while (ptr) {
                         |      |                ~~~
                         |      |                |
                         |      |                (58) following 'true' branch (when 'ptr' is non-NULL)...
                         |......
                         |   32 |                 if (fnmatch(ptr->fname, fname, FNM_PATHNAME) == 0) {
                         |      |                 ~~ ~
                         |      |                 |  |
                         |      |                 |  (60) following 'false' branch...
                         |      |                 (59) ...to here
                         |......
                         |   38 |                 if (strlen(fname) > ptr->len &&
                         |      |                 ~~
                         |      |                 |
                         |      |                 (61) ...to here
                         |......
                         |   48 |         if (found)
                         |      |            ~
                         |      |            |
                         |      |            (62) following 'false' branch (when 'found == 0')...
                         |
                       'filedb_find.part.0': event 63
                         |
                         |cc1:
                         | (63): ...to here
                         |
                  <------+
                  |
                'filedb_find': event 64
                  |
                  |cc1:
                  | (64): returning to 'filedb_find' from 'filedb_find.part.0'
                  |
           <------+
           |
         'filedb_add': events 65-66
           |
           |   58 |         if (filedb_find(head, fname))
           |      |            ~^~~~~~~~~~~~~~~~~~~~~~~~
           |      |            ||
           |      |            |(65) returning to 'filedb_add' from 'filedb_find'
           |      |            (66) following 'false' branch...
           |
         'filedb_add': event 67
           |
           |cc1:
           | (67): ...to here
           |
         'filedb_add': event 68
           |
           |cc1:
           | (68): calling 'filedb_add.part.0' from 'filedb_add'
           |
           +--> 'filedb_add.part.0': events 69-70
                  |
                  |   54 | FileDB *filedb_add(FileDB *head, const char *fname) {
                  |      |         ^~~~~~~~~~
                  |      |         |
                  |      |         (69) entry to 'filedb_add.part.0'
                  |......
                  |   63 |         if (!entry)
                  |      |            ~
                  |      |            |
                  |      |            (70) following 'false' branch (when 'entry' is non-NULL)...
                  |
                'filedb_add.part.0': event 71
                  |
                  |../include/common.h:39:28:
                  |   39 | #define errExit(msg)    do { char msgout[500]; snprintf(msgout, 500, "Error %s: %s:%d %s", msg, __FILE__, __LINE__, __FUNCTION__); perror(msgout); exit(1);} while (0)
                  |      |                            ^
                  |      |                            |
                  |      |                            (71) ...to here
filedb.c:64:17: note: in expansion of macro 'errExit'
                  |   64 |                 errExit("malloc");
                  |      |                 ^~~~~~~
                  |
                'filedb_add.part.0': events 72-73
                  |
                  |/usr/include/fortify/string.h:70:12:
                  |   70 |         if (__n > __b)
                  |      |            ^
                  |      |            |
                  |      |            (72) following 'false' branch (when '__b > 23')...
                  |   71 |                 __builtin_trap();
                  |   72 |         return __builtin_memset(__d, __c, __n);
                  |      |         ~~~~~~
                  |      |         |
                  |      |         (73) ...to here
                  |
                'filedb_add.part.0': events 74-75
                  |
                  |filedb.c:66:24:
                  |   66 |         entry->fname = strdup(fname);
                  |      |                        ^~~~~~~~~~~~~
                  |      |                        |
                  |      |                        (74) this call could return NULL
                  |   67 |         if (!entry->fname)
                  |      |            ~            
                  |      |            |
                  |      |            (75) following 'false' branch...
                  |
                'filedb_add.part.0': event 76
                  |
                  |../include/common.h:39:28:
                  |   39 | #define errExit(msg)    do { char msgout[500]; snprintf(msgout, 500, "Error %s: %s:%d %s", msg, __FILE__, __LINE__, __FUNCTION__); perror(msgout); exit(1);} while (0)
                  |      |                            ^
                  |      |                            |
                  |      |                            (76) ...to here
filedb.c:68:17: note: in expansion of macro 'errExit'
                  |   68 |                 errExit("strdup");
                  |      |                 ^~~~~~~
                  |
                'filedb_add.part.0': event 77
                  |
                  |   69 |         entry->len = strlen(entry->fname);
                  |      |                      ^~~~~~~~~~~~~~~~~~~~
                  |      |                      |
                  |      |                      (77) argument 1 ('strdup(fname)') from (74) could be NULL where non-null expected
                  |
note: argument 1 of 'strlen' must be non-null
In file included from /usr/include/fortify/string.h:22,
                 from ../include/common.h:31,
                 from fbuilder.h:23,
                 from filedb.c:21:
/usr/include/string.h:54:8: note: argument 1 of 'strlen' must be non-null
   54 | size_t strlen (const char *);
      |        ^~~~~~

@reinerh
Copy link
Collaborator Author

reinerh commented Oct 23, 2022

Thanks! I'll have a closer look at it.

@reinerh reinerh force-pushed the musl_warnings branch 2 times, most recently from 60a3405 to 1e0d4c1 Compare October 23, 2022 15:00
@reinerh
Copy link
Collaborator Author

reinerh commented Oct 23, 2022

The non-null warning looks like a false-positive to me:

        entry->fname = strdup(fname);
        if (!entry->fname)
                errExit("strdup");
        entry->len = strlen(entry->fname);

Even if strdup returns NULL, I don't see how NULL could get passed to strlen, as it would have exited...

in musl they are just redefines of the non-64 versions
@reinerh
Copy link
Collaborator Author

reinerh commented Oct 23, 2022

@rusty-snake I just checked with an alpine:edge container, which has gcc-12.
There it successfully builds with enabled analyzer.

@reinerh reinerh merged commit 730739d into master Oct 23, 2022
@reinerh reinerh deleted the musl_warnings branch October 23, 2022 15:31
@kmk3 kmk3 changed the title Fix musl warnings build: Fix musl warnings Nov 3, 2022
kmk3 added a commit that referenced this pull request Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done (on RELNOTES)
Development

Successfully merging this pull request may close these issues.

build: Some compiler warnings with musl
3 participants