cxong / tinydir

Lightweight, portable and easy to integrate C directory and file reader

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GCC11 warnings

lindsayad opened this issue · comments

In our repository idaholab/moose, we are getting the following warnings when compiling with gcc 11

In file included from /usr/include/string.h:519,
                 from /usr/include/c++/11/cstring:42,
                 from /home/lindad/projects/moose/libmesh/installed/include/Eigen/Core:286,
                 from /home/lindad/projects/moose/libmesh/installed/include/libmesh/type_vector.h:35,
                 from /home/lindad/projects/moose/libmesh/installed/include/libmesh/point.h:24,
                 from /home/lindad/projects/moose/libmesh/installed/include/libmesh/bounding_box.h:25,
                 from /home/lindad/projects/moose/framework/build/header_symlinks/MooseUtils.h:22,
                 from /home/lindad/projects/moose/framework/src/utils/MooseUtils.C:11:
In function ‘char* strncat(char*, const char*, size_t)’,
    inlined from ‘int tinydir_readfile(const tinydir_dir*, tinydir_file*)’ at /home/lindad/projects/moose/framework/contrib/tinydir/include/tinydir.h:334:10,
    inlined from ‘std::__cxx11::list<std::__cxx11::basic_string<char> > MooseUtils::listDir(std::string, bool)’ at /home/lindad/projects/moose/framework/src/utils/MooseUtils.C:701:21:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:135:34: warning: ‘char* __builtin___strncat_chk(char*, const char*, long unsigned int, long unsigned int)’ accessing between 3 and 1 bytes at offsets 0 and 4096 overlaps 1 byte at offset 4096 [-Wrestrict]
  135 |   return __builtin___strncat_chk (__dest, __src, __len,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
  136 |                                   __glibc_objsize (__dest));
      |                                   ~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /home/lindad/projects/moose/framework/src/utils/MooseUtils.C:24:
In function ‘void tinydir_close(tinydir_dir*)’,
    inlined from ‘void tinydir_close(tinydir_dir*)’ at /home/lindad/projects/moose/framework/contrib/tinydir/include/tinydir.h:215:6,
    inlined from ‘std::__cxx11::list<std::__cxx11::basic_string<char> > MooseUtils::listDir(std::string, bool)’ at /home/lindad/projects/moose/framework/src/utils/MooseUtils.C:709:16:
/home/lindad/projects/moose/framework/contrib/tinydir/include/tinydir.h:225:18: warning: ‘dir.tinydir_dir::_files’ may be used uninitialized [-Wmaybe-uninitialized]
  225 |         if (dir->_files != NULL)
      |             ~~~~~^~~~~~
/home/lindad/projects/moose/framework/src/utils/MooseUtils.C: In function ‘std::__cxx11::list<std::__cxx11::basic_string<char> > MooseUtils::listDir(std::string, bool)’:
/home/lindad/projects/moose/framework/src/utils/MooseUtils.C:693:15: note: ‘dir’ declared here
  693 |   tinydir_dir dir;

Is this user error or is this something that can be fixed on your end? This is our user code

std::list<std::string>
listDir(const std::string path, bool files_only)
{
  std::list<std::string> files;

  tinydir_dir dir;
  dir.has_next = 0; // Avoid a garbage value in has_next (clang StaticAnalysis)                                       
  tinydir_open(&dir, path.c_str());

  while (dir.has_next)
  {
    tinydir_file file;
    file.is_dir = 0; // Avoid a garbage value in is_dir (clang StaticAnalysis)                                        
    tinydir_readfile(&dir, &file);

    if (!files_only || !file.is_dir)
      files.push_back(path + "/" + file.name);

    tinydir_next(&dir);
  }

  tinydir_close(&dir);

  return files;
}
commented

We'll need to fix this in tinydir

commented

I've set up CI with GCC11 and I can't reproduce your error. Could you please double check your file? For example, it seems that the shadow warning is referring to moose/framework/src/utils/MooseUtils.C:693:15.
Can you create a minimal code example that reproduces the problem? Please include compiler flags as well

If I compile

#include "tinydir.h"
#include <string>
#include <list>

std::list<std::string>
listDir(const std::string path, bool files_only)
{
  std::list<std::string> files;

  tinydir_dir dir;
  dir.has_next = 0; // Avoid a garbage value in has_next (clang StaticAnalysis)                                       
  tinydir_open(&dir, path.c_str());

  while (dir.has_next)
  {
    tinydir_file file;
    file.is_dir = 0; // Avoid a garbage value in is_dir (clang StaticAnalysis)                                        
    tinydir_readfile(&dir, &file);

    if (!files_only || !file.is_dir)
      files.push_back(path + "/" + file.name);

    tinydir_next(&dir);
  }

  tinydir_close(&dir);

  return files;
}

then I get the warnings with the following compile line:

g++ -I/home/lindad/projects/moose/framework/contrib/tinydir/include tiny-dir-warning.cpp -O2 -o tiny-dir-warning.o -c -Wall

If I drop to -O1 optimization then I don't get the -Wmaybe-uninitialized warning, and if I drop to -O0, then I get no warnings at all which makes some sense because the warning messages explicitly mention inlining.

If these warnings are a direct result of inlining, and the functions are fine when not-inlined, then this seems like an issue with the compiler and not with tinydir ... ?

commented

please check the return value of tinydir functions; they return 0 on non-errors. This should fix some of the warnings.

For example, if I use if (tinydir_open(&dir, path.c_str()) != 0) return files; the warnings go away