• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <stdlib.h>
6 #include <string>
7 
8 #include "base/containers/span.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/logging.h"
13 #include "base/nix/mime_util_xdg.h"
14 
15 // Entry point for LibFuzzer.
LLVMFuzzerTestOneInput(const uint8_t * data_ptr,size_t size)16 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data_ptr, size_t size) {
17   // SAFETY: LibFuzzer provides a valid pointer/size pair.
18   auto data = UNSAFE_BUFFERS(base::span(data_ptr, size));
19 
20   base::ScopedTempDir temp_dir;
21   if (!temp_dir.CreateUniqueTempDir()) {
22     // Not a fuzzer error, so we return 0.
23     LOG(ERROR) << "Failed to create temp dir";
24     return 0;
25   }
26 
27   // The parser reads file $XDG_DATA_DIRS/mime/mime.cache.
28   setenv("XDG_DATA_DIRS", temp_dir.GetPath().value().c_str(), 1);
29   base::FilePath mime_dir = temp_dir.GetPath().Append("mime");
30   base::FilePath mime_cache = mime_dir.Append("mime.cache");
31   if (!base::CreateDirectory(mime_dir) || !base::WriteFile(mime_cache, data)) {
32     LOG(ERROR) << "Failed to create " << mime_cache;
33     // Not a fuzzer error, so we return 0.
34     return 0;
35   }
36 
37   base::FilePath dummy_path("foo.txt");
38   std::string type = base::nix::GetFileMimeType(dummy_path);
39   return 0;
40 }
41