hyperrealm / libconfig

C/C++ library for processing configuration files
https://hyperrealm.github.io/libconfig/
GNU Lesser General Public License v2.1
1.1k stars 359 forks source link

Suggest fuzzer for libconfig. #224

Open autofuzzoss opened 1 year ago

autofuzzoss commented 1 year ago

I suggest this fuzzer for continuous vulnerability checks.

/*
 * This fuzzer is generated by UTopia with some manual modifications.
 * (UTopia Project: https://github.com/Samsung/UTopia)
 */

#include "libconfig.h"
#include <FuzzedDataProvider.h>
#include <fstream>

bool saveFile(std::string Path, std::string Content) {
  std::ofstream OFS(Path);
  if (!OFS.is_open())
    return false;

  OFS << Content;
  return true;
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, uint32_t size) {
  config_t cfg;
  int ok;
  const char *str;
  FuzzedDataProvider provider(data, size);
  auto Input1 = provider.ConsumeRandomLengthString(10);
  auto Input2 = provider.ConsumeRandomLengthString(10);
  auto Input3 = provider.ConsumeRemainingBytesAsString();
  if (!saveFile("input", Input3))
    return 0;

  config_init(&cfg);
  config_set_include_dir(&cfg, Input1.c_str());

  ok = config_read_file(&cfg, "input");
  if (!ok) {
    config_error_text(&cfg);
    config_error_line(&cfg);
  }

  config_lookup_string(&cfg, Input2.c_str(), &str);
  config_destroy(&cfg);
  return 0;
}