Skip to content
This repository has been archived by the owner on Jan 2, 2021. It is now read-only.

error: ‘char* strncpy(char*, const char*, size_t)’ output truncated before terminating nul copying 4 bytes from a string of the same length [-Werror=stringop-truncation] #297

Open
zsong opened this issue Dec 22, 2018 · 9 comments

Comments

@zsong
Copy link

zsong commented Dec 22, 2018

mkdir -p build
cmake -Bbuild -H. -G Ninja -DCMAKE_INSTALL_PREFIX= 
-- The C compiler identification is GNU 8.2.0
-- The CXX compiler identification is GNU 8.2.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PythonInterp: /usr/bin/python (found version "2.7.15") 
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.11") 
-- Found PNG: /usr/lib/x86_64-linux-gnu/libpng.so (found version "1.6.34") 
-- Found LibXml2: /usr/lib/x86_64-linux-gnu/libxml2.so (found version "2.9.4") 
-- Configuring done
-- Generating done
ninja -C build 
ninja: Entering directory `build'
[104/537] Building CXX object Libraries/libcar/CMakeFiles/car.dir/Sources/Rendition.cpp.o
FAILED: Libraries/libcar/CMakeFiles/car.dir/Sources/Rendition.cpp.o 
/usr/bin/c++  -Dcar_EXPORTS -I../Libraries/libcar/Headers -I../Libraries/ext/Headers -I../Libraries/libbom/Headers -I../Libraries/libutil/Headers -std=c++11 -fno-rtti -fno-exceptions -fPIC   -Wall -Werror -Wno-multichar -Wno-sign-compare -fdiagnostics-color -MD -MT Libraries/libcar/CMakeFiles/car.dir/Sources/Rendition.cpp.o -MF Libraries/libcar/CMakeFiles/car.dir/Sources/Rendition.cpp.o.d -o Libraries/libcar/CMakeFiles/car.dir/Sources/Rendition.cpp.o -c ../Libraries/libcar/Sources/Rendition.cpp
../Libraries/libcar/Sources/Rendition.cpp: In member function ‘std::vector<unsigned char> car::Rendition::write() const’:
../Libraries/libcar/Sources/Rendition.cpp:532:12: error: ‘char* strncpy(char*, const char*, size_t)’ output truncated before terminating nul copying 4 bytes from a string of the same length [-Werror=stringop-truncation]
     strncpy(header.magic, "ISTC", 4);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
../Libraries/libcar/Sources/Rendition.cpp:679:16: error: ‘char* strncpy(char*, const char*, size_t)’ output truncated before terminating nul copying 4 bytes from a string of the same length [-Werror=stringop-truncation]
         strncpy(raw_header.magic, "DWAR", 4);
         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1plus: all warnings being treated as errors
[106/537] Building CXX object Libraries/libcar/CMakeFiles/car.dir/Sources/Writer.cpp.o
FAILED: Libraries/libcar/CMakeFiles/car.dir/Sources/Writer.cpp.o 
/usr/bin/c++  -Dcar_EXPORTS -I../Libraries/libcar/Headers -I../Libraries/ext/Headers -I../Libraries/libbom/Headers -I../Libraries/libutil/Headers -std=c++11 -fno-rtti -fno-exceptions -fPIC   -Wall -Werror -Wno-multichar -Wno-sign-compare -fdiagnostics-color -MD -MT Libraries/libcar/CMakeFiles/car.dir/Sources/Writer.cpp.o -MF Libraries/libcar/CMakeFiles/car.dir/Sources/Writer.cpp.o.d -o Libraries/libcar/CMakeFiles/car.dir/Sources/Writer.cpp.o -c ../Libraries/libcar/Sources/Writer.cpp
../Libraries/libcar/Sources/Writer.cpp: In member function ‘void car::Writer::write() const’:
../Libraries/libcar/Sources/Writer.cpp:101:12: error: ‘char* strncpy(char*, const char*, size_t)’ output truncated before terminating nul copying 4 bytes from a string of the same length [-Werror=stringop-truncation]
     strncpy(header->magic, "RATC", 4);
     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
../Libraries/libcar/Sources/Writer.cpp:131:14: error: ‘char* strncpy(char*, const char*, size_t)’ output truncated before terminating nul copying 4 bytes from a string of the same length [-Werror=stringop-truncation]
       strncpy(keyfmt->magic, "tmfk", 4);
       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
cc1plus: all warnings being treated as errors
[109/537] Building CXX object Libraries/libcar/CMakeFiles/test_car_Rendition.dir/Tests/test_Rendition.cpp.o
ninja: build stopped: subcommand failed.
make: *** [Makefile:22: all] Error 1
@jacktlange
Copy link

@zsong I have run into an identical error. Have you found a solution?

@tienex
Copy link

tienex commented Jan 4, 2019

Replace the strncpy with memcpy

@jakobkukla
Copy link

@tienex Where do I have to replace it?

@tienex
Copy link

tienex commented Jan 21, 2019

The error contains the file name and the line, open that file go to that line and replace it.

@jakobkukla
Copy link

Thanks!

@jakobkukla
Copy link

Thanks it worked. I got it working just to found out that it actually can't build anything on linux haha.

@liangti
Copy link

liangti commented Dec 5, 2019

I just coincidentally walk into here lead by google search. So that the error said the output was truncated before the null terminating, since "ISTC" actually contains 5 chars "I" "S" "T" "C" and 0 (try sizeof("ISTC")). It's a new checker for string truncated provided by gcc-8

https://developers.redhat.com/blog/2018/05/24/detecting-string-truncation-with-gcc-8/

@NaruFGT
Copy link

NaruFGT commented Jan 3, 2020

@liangti the intention is to only copy 4 bytes, the Compiled Asset Archive format has magic bytes describing the rendition. A null terminating byte would clobber the next struct member.
The correct fix is to use std::memcpy() rather than strncpy() for these non-null terminated fixed length string literals.
See car_format.h and #311 for more details.

@NaruFGT
Copy link

NaruFGT commented Jan 3, 2020

This is entirely pedantic though, and you could also continue to use strncpy() if you'd like by passing -Wno-error=stringop-truncation to your compiler, see CMakeLists.txt and find add_compile_options.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants