O2 on Ubuntu with -Werror gives some `error: '%s' directive output may be truncated`

Hi,

Trying to build O2 on a Ubuntu 20.04 (virtual) machine with env variable ALIBUILD_O2_TESTS set (and hence -Werror extra compilation option) the build stops for errors like the following (there are a few places like that actually, in some cases the destination string is indeed possibly must smaller than required by the pattern) :

/home/laurent/alice/sw/SOURCES/O2/o2-2132-get-rid-of-test-wrapper/0/Detectors/TOF/base/src/Geo.cxx:199:36: error: '%s' directive output may be truncated writing up to 99 bytes into a region of size between 0 and 198 [-Werror=format-truncation=]
  199 |   snprintf(path, 2 * kSize, "%s/%s/%s", string1, string2, string3);
      |                                    ^~

It seems the usage of snprintf is quite routine in the geometry-related code.

Is there a particular reason to use those instead of safer alternatives (e.g. fmt::format), or is just “legacy” ?

Should they be fixed or should I just add e.g. -Wno-error=format-truncation on Ubuntu for now ?

Hi @laphecet

This warning pops up in ubuntu even w/o ALIBUILD_O2_TESTS. I remember that I’ve checked this particular line and concluded that there is no risk of the overflow. Anyway, just to mute this warning I’ll fix this line.
But, in general the snprinft is used in the legacy code from the AliRoot, not sure we want to touch it.

Actually there are more places with this kind of warning (or error if running with -Werror). If we don’t want to correct them I would then propose to simply add -Wno-error=format-truncation to o2.sh, so that Ubuntu builds with ALIBUILD_O2_TESTS will actually succeed (I’ve tested that locally). Do you agree ?

Could you post here these warnings? If there are only few, I would rather correct them than change to options.

Actually seems they are all within a single source file, so indeed might worth a fix simply.

[ (python) alice ]~/alice/sw/BUILD/O2-latest$ grep warning log
/home/laurent/alice/sw/SOURCES/O2/dev/0/Detectors/Passive/src/Pipe.cxx:2835:27: warning: '%s' directive output may be truncated writing up to 63 bytes into a region of size between 0 and 63 [-Wformat-truncation=]
/home/laurent/alice/sw/SOURCES/O2/dev/0/Detectors/Passive/src/Pipe.cxx:2848:27: warning: '%s' directive output may be truncated writing up to 63 bytes into a region of size between 0 and 63 [-Wformat-truncation=]
/home/laurent/alice/sw/SOURCES/O2/dev/0/Detectors/Passive/src/Pipe.cxx:2916:27: warning: '%s' directive output may be truncated writing up to 63 bytes into a region of size between 0 and 63 [-Wformat-truncation=]
/home/laurent/alice/sw/SOURCES/O2/dev/0/Detectors/Passive/src/Pipe.cxx:2933:27: warning: '%s' directive output may be truncated writing up to 63 bytes into a region of size between 0 and 63 [-Wformat-truncation=]
/home/laurent/alice/sw/SOURCES/O2/dev/0/Detectors/Passive/src/Pipe.cxx:2943:27: warning: '%s' directive output may be truncated writing up to 63 bytes into a region of size between 0 and 63 [-Wformat-truncation=]
/home/laurent/alice/sw/SOURCES/O2/dev/0/Detectors/Passive/src/Pipe.cxx:2967:27: warning: '%s' directive output may be truncated writing up to 63 bytes into a region of size between 0 and 63 [-Wformat-truncation=]
/home/laurent/alice/sw/SOURCES/O2/dev/0/Detectors/Passive/src/Pipe.cxx:2982:26: warning: ':t1-' directive output may be truncated writing 4 bytes into a region of size between 1 and 64 [-Wformat-truncation=]
/home/laurent/alice/sw/SOURCES/O2/dev/0/Detectors/Passive/src/Pipe.cxx:2989:26: warning: ':t2-' directive output may be truncated writing 4 bytes into a region of size between 1 and 64 [-Wformat-truncation=]

OK, thanks, snprintf -> fmt::format to suppress warnings by shahor02 · Pull Request #6482 · AliceO2Group/AliceO2 · GitHub should fix those.