diff --git a/.travis-qemu.sh b/.travis-qemu.sh index d9ac09a3..94e60973 100755 --- a/.travis-qemu.sh +++ b/.travis-qemu.sh @@ -74,7 +74,7 @@ if [ "${ARCH}" = "arm" ]; then arm-linux-gnueabihf-gcc-4.6 -v # Crosscompile CMocka - pushd $HOME + pushd ${HOME} git clone git://git.cryptomilk.org/projects/cmocka.git mkdir cmocka_build && cd cmocka_build cmake ../cmocka \ @@ -86,7 +86,7 @@ if [ "${ARCH}" = "arm" ]; then popd # Crosscompile libcbor - cmake $SOURCE \ + cmake ${SOURCE} \ -DCBOR_CUSTOM_ALLOC=ON \ -DCMAKE_BUILD_TYPE=Debug \ -DWITH_TESTS=ON \ @@ -100,7 +100,7 @@ if [ "${ARCH}" = "arm" ]; then else # Proceed as normal gem install coveralls-lcov - pushd $HOME + pushd ${HOME} git clone git://git.cryptomilk.org/projects/cmocka.git mkdir cmocka_build && cd cmocka_build cmake ../cmocka @@ -113,7 +113,13 @@ else echo "Running tests" cppcheck . --error-exitcode=1 --suppressions cppcheck_suppressions.txt --force - cmake . -DCBOR_CUSTOM_ALLOC=ON -DCMAKE_BUILD_TYPE=Debug -DWITH_TESTS=ON -DCMAKE_PREFIX_PATH=$HOME/usr/local + cmake \ + -DCBOR_CUSTOM_ALLOC=ON \ + -DCMAKE_BUILD_TYPE=Debug \ + -DSANITIZE=OFF \ + -DWITH_TESTS=ON \ + -DCMAKE_PREFIX_PATH=${HOME}/usr/local \ + . make VERBOSE=1 ctest -VV diff --git a/.travis.yml b/.travis.yml index f287883a..26a795c4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,8 +19,7 @@ matrix: - lcov - cmake - cmake-data - env: CC="clang-3.6" CXX="clang++-3.6" - - compiler: gcc + - compiler: gcc-6 addons: apt: sources: @@ -35,7 +34,6 @@ matrix: - lcov - cmake - cmake-data - env: CC="gcc-6" CXX="g++-6" # Temporarily disabled due to slow performance and problems with Raspbian # chroot (https://travis-ci.org/PJK/libcbor/jobs/281748138) # - env: ARCH=arm diff --git a/CMakeLists.txt b/CMakeLists.txt index b45cba5e..7dfabf2e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,6 +47,7 @@ if(PRINT_FUZZ) add_definitions(-DPRINT_FUZZ) endif(PRINT_FUZZ) +option(SANITIZE "Enable ASan & a few compatible sanitizers in Debug mode" ON) set(CPACK_GENERATOR "DEB" "TGZ" "RPM") set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "amd64") @@ -72,7 +73,11 @@ else() set(CBOR_RESTRICT_SPECIFIER "restrict") set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -Wall -pedantic -g -ggdb -DDEBUG=true") - set(CMAKE_C_FLAGS_RELEASE "-O3 -flto -Wall -pedantic -DNDEBUG") + set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -flto -Wall -pedantic -DNDEBUG") + + if(SANITIZE) + set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fsanitize=undefined -fsanitize=address -fsanitize=bounds -fsanitize=alignment") + endif() endif() set(CMAKE_EXE_LINKER_FLAGS_DEBUG "-g") diff --git a/examples/streaming_parser.c b/examples/streaming_parser.c index 4ecad659..bc8dc650 100644 --- a/examples/streaming_parser.c +++ b/examples/streaming_parser.c @@ -28,14 +28,7 @@ bool key_found = false; void find_string(void * _ctx, cbor_data buffer, size_t len) { if (key_found) { - char *tmp = calloc(1, len + 1); - if (!tmp) { - fprintf(stderr, "calloc failed\n"); - exit(1); - } - memcpy(tmp, buffer, len); - printf("Found the value: %s\n", tmp); - free(tmp); + printf("Found the value: %.*s\n", (int) len, buffer); key_found = false; } else if (len == strlen(key)) { key_found = (memcmp(key, buffer, len) == 0);