-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fuzzer for base::nix::GetFileMimeType()
Change-Id: I0160bba4f9a5e9feb99b02a9de052f946fd98066 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4982470 Reviewed-by: Thomas Anderson <thomasanderson@chromium.org> Commit-Queue: Joel Hockey <joelhockey@chromium.org> Cr-Commit-Position: refs/heads/main@{#1216720}
- Loading branch information
Joel Hockey
authored and
Chromium LUCI CQ
committed
Oct 29, 2023
1 parent
e6151d5
commit 264c759
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include <stdlib.h> | ||
#include <string> | ||
|
||
#include "base/containers/span.h" | ||
#include "base/files/file_path.h" | ||
#include "base/files/file_util.h" | ||
#include "base/files/scoped_temp_dir.h" | ||
#include "base/logging.h" | ||
#include "base/nix/mime_util_xdg.h" | ||
|
||
// Entry point for LibFuzzer. | ||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | ||
base::ScopedTempDir temp_dir; | ||
if (!temp_dir.CreateUniqueTempDir()) { | ||
// Not a fuzzer error, so we return 0. | ||
LOG(ERROR) << "Failed to create temp dir"; | ||
return 0; | ||
} | ||
|
||
// The parser reads file $XDG_DATA_DIRS/mime/mime.cache. | ||
setenv("XDG_DATA_DIRS", temp_dir.GetPath().value().c_str(), 1); | ||
base::FilePath mime_dir = temp_dir.GetPath().Append("mime"); | ||
base::FilePath mime_cache = mime_dir.Append("mime.cache"); | ||
if (!base::CreateDirectory(mime_dir) || | ||
!base::WriteFile(mime_cache, base::make_span(data, size))) { | ||
LOG(ERROR) << "Failed to create " << mime_cache; | ||
// Not a fuzzer error, so we return 0. | ||
return 0; | ||
} | ||
|
||
base::FilePath dummy_path("foo.txt"); | ||
std::string type = base::nix::GetFileMimeType(dummy_path); | ||
return 0; | ||
} |