wtsi-npg / baton

iRODS client programs and API
http://wtsi-npg.github.io/baton
GNU General Public License v2.0
19 stars 19 forks source link

Warnings found when building with new-ish C compilers #263

Open daviesrob opened 2 years ago

daviesrob commented 2 years ago

While attempting some builds with recent compilers I got the following warnings:

clang 13 doesn't like this line:

src/write.c:279:11: warning: explicitly assigning value of variable of type 'int' to itself [-Wself-assign]
    flags = flags;
    ~~~~~ ^ ~~~~~

gcc 12.1, on the other hand has no trouble with assigning a variable to itself, but doesn't like the use of strncpy() in src/query.c prepare_obj_list and prepare_obj_repl_list:

src/query.c: In function ‘prepare_obj_list’:
src/query.c:189:5: warning: ‘strncpy’ specified bound depends on the length of the source argument [-Wstringop-truncation]
  189 |     strncpy(path1, path, len);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~
src/query.c:182:18: note: length computed here
  182 |     size_t len = strlen(path) + 1;
      |                  ^~~~~~~~~~~~
src/query.c:190:5: warning: ‘strncpy’ specified bound depends on the length of the source argument [-Wstringop-truncation]
  190 |     strncpy(path2, path, len);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~
src/query.c:182:18: note: length computed here
  182 |     size_t len = strlen(path) + 1;
      |                  ^~~~~~~~~~~~
src/query.c: In function ‘prepare_obj_repl_list’:
src/query.c:285:5: warning: ‘strncpy’ specified bound depends on the length of the source argument [-Wstringop-truncation]
  285 |     strncpy(path1, path, len);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~
src/query.c:278:18: note: length computed here
  278 |     size_t len = strlen(path) + 1;
      |                  ^~~~~~~~~~~~
src/query.c:286:5: warning: ‘strncpy’ specified bound depends on the length of the source argument [-Wstringop-truncation]
  286 |     strncpy(path2, path, len);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~
src/query.c:278:18: note: length computed here
  278 |     size_t len = strlen(path) + 1;
      |                  ^~~~~~~~~~~~

These are arguably a false positive, but there isn't much sign of that being fixed. In both cases it looks like it would be easier to use strdup() though, which would fix the warnings and simplify the code a bit.