| 36 | } // namespace |
| 37 | |
| 38 | FUZZ_TARGET(locale) |
| 39 | { |
| 40 | FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); |
| 41 | const std::string locale_identifier = ConsumeLocaleIdentifier(fuzzed_data_provider); |
| 42 | if (!IsAvailableLocale(locale_identifier)) { |
| 43 | return; |
| 44 | } |
| 45 | const char* c_locale = std::setlocale(LC_ALL, "C"); |
| 46 | assert(c_locale != nullptr); |
| 47 | |
| 48 | const std::string random_string = fuzzed_data_provider.ConsumeRandomLengthString(5); |
| 49 | int32_t parseint32_out_without_locale; |
| 50 | const bool parseint32_without_locale = ParseInt32(random_string, &parseint32_out_without_locale); |
| 51 | int64_t parseint64_out_without_locale; |
| 52 | const bool parseint64_without_locale = ParseInt64(random_string, &parseint64_out_without_locale); |
| 53 | const int64_t random_int64 = fuzzed_data_provider.ConsumeIntegral<int64_t>(); |
| 54 | const std::string tostring_without_locale = ToString(random_int64); |
| 55 | // The variable `random_int32` is no longer used, but the harness still needs to |
| 56 | // consume the same data that it did previously to not invalidate existing seeds. |
| 57 | const int32_t random_int32 = fuzzed_data_provider.ConsumeIntegral<int32_t>(); |
| 58 | (void)random_int32; |
| 59 | const std::string strprintf_int_without_locale = strprintf("%d", random_int64); |
| 60 | const double random_double = fuzzed_data_provider.ConsumeFloatingPoint<double>(); |
| 61 | const std::string strprintf_double_without_locale = strprintf("%f", random_double); |
| 62 | |
| 63 | const char* new_locale = std::setlocale(LC_ALL, locale_identifier.c_str()); |
| 64 | assert(new_locale != nullptr); |
| 65 | |
| 66 | int32_t parseint32_out_with_locale; |
| 67 | const bool parseint32_with_locale = ParseInt32(random_string, &parseint32_out_with_locale); |
| 68 | assert(parseint32_without_locale == parseint32_with_locale); |
| 69 | if (parseint32_without_locale) { |
| 70 | assert(parseint32_out_without_locale == parseint32_out_with_locale); |
| 71 | } |
| 72 | int64_t parseint64_out_with_locale; |
| 73 | const bool parseint64_with_locale = ParseInt64(random_string, &parseint64_out_with_locale); |
| 74 | assert(parseint64_without_locale == parseint64_with_locale); |
| 75 | if (parseint64_without_locale) { |
| 76 | assert(parseint64_out_without_locale == parseint64_out_with_locale); |
| 77 | } |
| 78 | const std::string tostring_with_locale = ToString(random_int64); |
| 79 | assert(tostring_without_locale == tostring_with_locale); |
| 80 | const std::string strprintf_int_with_locale = strprintf("%d", random_int64); |
| 81 | assert(strprintf_int_without_locale == strprintf_int_with_locale); |
| 82 | const std::string strprintf_double_with_locale = strprintf("%f", random_double); |
| 83 | assert(strprintf_double_without_locale == strprintf_double_with_locale); |
| 84 | |
| 85 | const std::locale current_cpp_locale; |
| 86 | assert(current_cpp_locale == std::locale::classic()); |
| 87 | } |
nothing calls this directly
no test coverage detected