mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-09 21:00:06 +00:00
Apply clang format and other minor formatting tweaks
This commit is contained in:
parent
ab0ba8a9d0
commit
2711cb1672
@ -192,7 +192,7 @@ if (BUILD_SHARED_LIBS)
|
|||||||
target_compile_definitions(fmt PRIVATE FMT_EXPORT INTERFACE FMT_SHARED)
|
target_compile_definitions(fmt PRIVATE FMT_EXPORT INTERFACE FMT_SHARED)
|
||||||
endif ()
|
endif ()
|
||||||
if (FMT_SAFE_DURATION_CAST)
|
if (FMT_SAFE_DURATION_CAST)
|
||||||
target_compile_definitions(fmt PUBLIC FMT_SAFE_DURATION_CAST)
|
target_compile_definitions(fmt PUBLIC FMT_SAFE_DURATION_CAST)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
add_library(fmt-header-only INTERFACE)
|
add_library(fmt-header-only INTERFACE)
|
||||||
@ -275,7 +275,7 @@ if (FMT_TEST)
|
|||||||
add_subdirectory(test)
|
add_subdirectory(test)
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
# control fuzzing independent of the unit tests
|
# Control fuzzing independent of the unit tests.
|
||||||
if (FMT_FUZZ)
|
if (FMT_FUZZ)
|
||||||
add_subdirectory(test/fuzzing)
|
add_subdirectory(test/fuzzing)
|
||||||
endif ()
|
endif ()
|
||||||
|
@ -90,7 +90,7 @@ cmake_flags = [
|
|||||||
'-DCMAKE_CXX_STANDARD=' + standard
|
'-DCMAKE_CXX_STANDARD=' + standard
|
||||||
]
|
]
|
||||||
|
|
||||||
# make sure the fuzzers still compile
|
# Make sure the fuzzers still compile.
|
||||||
if 'ENABLE_FUZZING' in os.environ:
|
if 'ENABLE_FUZZING' in os.environ:
|
||||||
cmake_flags += ['-DFMT_FUZZ=ON', '-DFMT_FUZZ_LINKMAIN=On']
|
cmake_flags += ['-DFMT_FUZZ=ON', '-DFMT_FUZZ_LINKMAIN=On']
|
||||||
|
|
||||||
|
@ -6,33 +6,33 @@
|
|||||||
# (note that libFuzzer can also reproduce, just pass it the files)
|
# (note that libFuzzer can also reproduce, just pass it the files)
|
||||||
option(FMT_FUZZ_LINKMAIN "enables the reproduce mode, instead of libFuzzer" On)
|
option(FMT_FUZZ_LINKMAIN "enables the reproduce mode, instead of libFuzzer" On)
|
||||||
|
|
||||||
#for oss-fuzz - insert $LIB_FUZZING_ENGINE into the link flags, but only for
|
# For oss-fuzz - insert $LIB_FUZZING_ENGINE into the link flags, but only for
|
||||||
#the fuzz targets, otherwise the cmake configuration step fails.
|
# the fuzz targets, otherwise the cmake configuration step fails.
|
||||||
set(FMT_FUZZ_LDFLAGS "" CACHE STRING "LDFLAGS for the fuzz targets")
|
set(FMT_FUZZ_LDFLAGS "" CACHE STRING "LDFLAGS for the fuzz targets")
|
||||||
|
|
||||||
#find all fuzzers.
|
# Find all fuzzers.
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
chrono_duration.cpp
|
chrono_duration.cpp
|
||||||
named_arg.cpp
|
named_arg.cpp
|
||||||
one_arg.cpp
|
one_arg.cpp
|
||||||
sprintf.cpp
|
sprintf.cpp
|
||||||
two_args.cpp
|
two_args.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
macro(implement_fuzzer sourcefile)
|
macro(implement_fuzzer sourcefile)
|
||||||
get_filename_component(basename ${sourcefile} NAME_WE)
|
get_filename_component(basename ${sourcefile} NAME_WE)
|
||||||
set(name fuzzer_${basename})
|
set(name fuzzer_${basename})
|
||||||
add_executable(${name} ${sourcefile} fuzzer_common.h)
|
add_executable(${name} ${sourcefile} fuzzer_common.h)
|
||||||
if(FMT_FUZZ_LINKMAIN)
|
if (FMT_FUZZ_LINKMAIN)
|
||||||
target_sources(${name} PRIVATE main.cpp)
|
target_sources(${name} PRIVATE main.cpp)
|
||||||
endif()
|
endif ()
|
||||||
target_link_libraries(${name} PRIVATE fmt)
|
target_link_libraries(${name} PRIVATE fmt)
|
||||||
if(FMT_FUZZ_LDFLAGS)
|
if (FMT_FUZZ_LDFLAGS)
|
||||||
target_link_libraries(${name} PRIVATE ${FMT_FUZZ_LDFLAGS})
|
target_link_libraries(${name} PRIVATE ${FMT_FUZZ_LDFLAGS})
|
||||||
endif()
|
endif ()
|
||||||
target_compile_features(${name} PRIVATE cxx_generic_lambdas)
|
target_compile_features(${name} PRIVATE cxx_generic_lambdas)
|
||||||
endmacro()
|
endmacro ()
|
||||||
|
|
||||||
foreach(X IN ITEMS ${SOURCES})
|
foreach (X IN ITEMS ${SOURCES})
|
||||||
implement_fuzzer(${X})
|
implement_fuzzer(${X})
|
||||||
endforeach()
|
endforeach ()
|
||||||
|
@ -1,18 +1,27 @@
|
|||||||
# FMT Fuzzer
|
# FMT Fuzzer
|
||||||
Fuzzing has revealed [several bugs](https://github.com/fmtlib/fmt/issues?&q=is%3Aissue+fuzz) in fmt. It is a part of the continous fuzzing at [oss-fuzz](https://github.com/google/oss-fuzz)
|
|
||||||
|
|
||||||
The source code is modified to make the fuzzing possible without locking up on resource exhaustion:
|
Fuzzing has revealed [several bugs](https://github.com/fmtlib/fmt/issues?&q=is%3Aissue+fuzz)
|
||||||
|
in fmt. It is a part of the continous fuzzing at
|
||||||
|
[oss-fuzz](https://github.com/google/oss-fuzz).
|
||||||
|
|
||||||
|
The source code is modified to make the fuzzing possible without locking up on
|
||||||
|
resource exhaustion:
|
||||||
```cpp
|
```cpp
|
||||||
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
||||||
if(spec.precision>100000) {
|
if(spec.precision>100000) {
|
||||||
throw std::runtime_error("fuzz mode - avoiding large precision");
|
throw std::runtime_error("fuzz mode - avoiding large precision");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
```
|
```
|
||||||
This macro is the defacto standard for making fuzzing practically possible, see [the libFuzzer documentation](https://llvm.org/docs/LibFuzzer.html#fuzzer-friendly-build-mode).
|
This macro is the defacto standard for making fuzzing practically possible, see
|
||||||
|
[the libFuzzer documentation](https://llvm.org/docs/LibFuzzer.html#fuzzer-friendly-build-mode).
|
||||||
|
|
||||||
## Running the fuzzers locally
|
## Running the fuzzers locally
|
||||||
There is a [helper script](build.sh) to build the fuzzers, which has only been tested on Debian and Ubuntu linux so far. There should be no problems fuzzing on Windows (using clang>=8) or on Mac, but the script will probably not work out of the box.
|
|
||||||
|
There is a [helper script](build.sh) to build the fuzzers, which has only been
|
||||||
|
tested on Debian and Ubuntu linux so far. There should be no problems fuzzing on
|
||||||
|
Windows (using clang>=8) or on Mac, but the script will probably not work out of
|
||||||
|
the box.
|
||||||
|
|
||||||
Something along
|
Something along
|
||||||
```sh
|
```sh
|
||||||
|
@ -4,9 +4,9 @@
|
|||||||
// Copyright (c) 2019, Paul Dreik
|
// Copyright (c) 2019, Paul Dreik
|
||||||
// License: see LICENSE.rst in the fmt root directory
|
// License: see LICENSE.rst in the fmt root directory
|
||||||
|
|
||||||
#include <cstring> // memcpy
|
#include <cstdint> // std::uint8_t
|
||||||
#include <type_traits> // trivially copyable
|
#include <cstring> // memcpy
|
||||||
#include <cstdint> // std::uint8_t
|
#include <type_traits> // trivially copyable
|
||||||
|
|
||||||
// one can format to either a string, or a buf. buf is faster,
|
// one can format to either a string, or a buf. buf is faster,
|
||||||
// but one may be interested in formatting to a string instead to
|
// but one may be interested in formatting to a string instead to
|
||||||
@ -25,36 +25,33 @@
|
|||||||
// is likely interesting.
|
// is likely interesting.
|
||||||
// For this, we must know the size of the largest possible type in use.
|
// For this, we must know the size of the largest possible type in use.
|
||||||
|
|
||||||
// There are some problems on travis, claiming Nfixed is not a constant expression
|
// There are some problems on travis, claiming Nfixed is not a constant
|
||||||
// which seems to be an issue with older versions of libstdc++
|
// expression which seems to be an issue with older versions of libstdc++
|
||||||
#if _GLIBCXX_RELEASE >= 7
|
#if _GLIBCXX_RELEASE >= 7
|
||||||
# include <algorithm>
|
# include <algorithm>
|
||||||
namespace fmt_fuzzer {
|
namespace fmt_fuzzer {
|
||||||
constexpr auto Nfixed = std::max(sizeof(long double), sizeof(std::intmax_t));
|
constexpr auto Nfixed = std::max(sizeof(long double), sizeof(std::intmax_t));
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
namespace fmt_fuzzer {
|
namespace fmt_fuzzer {
|
||||||
constexpr auto Nfixed=16;
|
constexpr auto Nfixed = 16;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace fmt_fuzzer {
|
namespace fmt_fuzzer {
|
||||||
// view data as a c char pointer.
|
// view data as a c char pointer.
|
||||||
template <typename T>
|
template <typename T> inline const char* as_chars(const T* data) {
|
||||||
inline const char* as_chars(const T* data) {
|
return static_cast<const char*>(static_cast<const void*>(data));
|
||||||
return static_cast<const char*>(static_cast<const void*>(data));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// view data as a byte pointer
|
// view data as a byte pointer
|
||||||
template <typename T>
|
template <typename T> inline const std::uint8_t* as_bytes(const T* data) {
|
||||||
inline const std::uint8_t* as_bytes(const T* data) {
|
return static_cast<const std::uint8_t*>(static_cast<const void*>(data));
|
||||||
return static_cast<const std::uint8_t*>(static_cast<const void*>(data));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// blits bytes from Data to form an (assumed trivially constructible) object
|
// blits bytes from Data to form an (assumed trivially constructible) object
|
||||||
// of type Item
|
// of type Item
|
||||||
template <class Item>
|
template <class Item> inline Item assignFromBuf(const std::uint8_t* Data) {
|
||||||
inline Item assignFromBuf(const std::uint8_t* Data) {
|
|
||||||
Item item{};
|
Item item{};
|
||||||
std::memcpy(&item, Data, sizeof(Item));
|
std::memcpy(&item, Data, sizeof(Item));
|
||||||
return item;
|
return item;
|
||||||
@ -65,7 +62,6 @@ template <> inline bool assignFromBuf<bool>(const std::uint8_t* Data) {
|
|||||||
return !!Data[0];
|
return !!Data[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace fmt_fuzzer
|
} // namespace fmt_fuzzer
|
||||||
|
|
||||||
|
#endif // FUZZER_COMMON_H
|
||||||
#endif // FUZZER_COMMON_H
|
|
||||||
|
@ -11,7 +11,7 @@ int main(int argc, char* argv[]) {
|
|||||||
assert(in);
|
assert(in);
|
||||||
in.seekg(0, std::ios_base::end);
|
in.seekg(0, std::ios_base::end);
|
||||||
const auto pos = in.tellg();
|
const auto pos = in.tellg();
|
||||||
assert(pos>=0);
|
assert(pos >= 0);
|
||||||
in.seekg(0, std::ios_base::beg);
|
in.seekg(0, std::ios_base::beg);
|
||||||
std::vector<char> buf(static_cast<std::size_t>(pos));
|
std::vector<char> buf(static_cast<std::size_t>(pos));
|
||||||
in.read(buf.data(), static_cast<long>(buf.size()));
|
in.read(buf.data(), static_cast<long>(buf.size()));
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
template <typename Item1>
|
template <typename Item1>
|
||||||
void invoke_fmt(const uint8_t* Data, std::size_t Size, unsigned int argsize) {
|
void invoke_fmt(const uint8_t* Data, std::size_t Size, unsigned int argsize) {
|
||||||
constexpr auto N1 = sizeof(Item1);
|
constexpr auto N1 = sizeof(Item1);
|
||||||
static_assert (N1<=fmt_fuzzer::Nfixed,"Nfixed too small");
|
static_assert(N1 <= fmt_fuzzer::Nfixed, "Nfixed too small");
|
||||||
if (Size <= fmt_fuzzer::Nfixed) {
|
if (Size <= fmt_fuzzer::Nfixed) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -126,4 +126,3 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, std::size_t Size) {
|
|||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ using fmt_fuzzer::Nfixed;
|
|||||||
template <typename Item>
|
template <typename Item>
|
||||||
void invoke_fmt(const uint8_t* Data, std::size_t Size) {
|
void invoke_fmt(const uint8_t* Data, std::size_t Size) {
|
||||||
constexpr auto N = sizeof(Item);
|
constexpr auto N = sizeof(Item);
|
||||||
static_assert (N<=Nfixed,"Nfixed is too small");
|
static_assert(N <= Nfixed, "Nfixed is too small");
|
||||||
if (Size <= Nfixed) {
|
if (Size <= Nfixed) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -43,7 +43,7 @@ void invoke_fmt(const uint8_t* Data, std::size_t Size) {
|
|||||||
void invoke_fmt_time(const uint8_t* Data, std::size_t Size) {
|
void invoke_fmt_time(const uint8_t* Data, std::size_t Size) {
|
||||||
using Item = std::time_t;
|
using Item = std::time_t;
|
||||||
constexpr auto N = sizeof(Item);
|
constexpr auto N = sizeof(Item);
|
||||||
static_assert (N<=Nfixed,"Nfixed too small");
|
static_assert(N <= Nfixed, "Nfixed too small");
|
||||||
if (Size <= Nfixed) {
|
if (Size <= Nfixed) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
#include "fuzzer_common.h"
|
#include "fuzzer_common.h"
|
||||||
|
|
||||||
constexpr auto Nfixed=fmt_fuzzer::Nfixed;
|
constexpr auto Nfixed = fmt_fuzzer::Nfixed;
|
||||||
|
|
||||||
template <typename Item1, typename Item2>
|
template <typename Item1, typename Item2>
|
||||||
void invoke_fmt(const uint8_t* Data, std::size_t Size) {
|
void invoke_fmt(const uint8_t* Data, std::size_t Size) {
|
||||||
@ -18,11 +18,11 @@ void invoke_fmt(const uint8_t* Data, std::size_t Size) {
|
|||||||
if (Size <= Nfixed + Nfixed) {
|
if (Size <= Nfixed + Nfixed) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const Item1 item1=fmt_fuzzer::assignFromBuf<Item1>(Data);
|
const Item1 item1 = fmt_fuzzer::assignFromBuf<Item1>(Data);
|
||||||
Data += Nfixed;
|
Data += Nfixed;
|
||||||
Size -= Nfixed;
|
Size -= Nfixed;
|
||||||
|
|
||||||
const Item2 item2=fmt_fuzzer::assignFromBuf<Item2>(Data);
|
const Item2 item2 = fmt_fuzzer::assignFromBuf<Item2>(Data);
|
||||||
Data += Nfixed;
|
Data += Nfixed;
|
||||||
Size -= Nfixed;
|
Size -= Nfixed;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user