Skip to content

Commit

Permalink
Improve xml loading method in fuzz_XMLProfiles (#4685)
Browse files Browse the repository at this point in the history
* Refs #20732: Change xml loading method to avoid copy to a file in fuzz_XMLProfiles

Signed-off-by: elianalf <[email protected]>

* Refs #20732: Remove methods no longer used

Signed-off-by: elianalf <[email protected]>

* Refs #20732: Apply suggestions

Signed-off-by: elianalf <[email protected]>

---------

Signed-off-by: elianalf <[email protected]>
  • Loading branch information
elianalf authored Apr 16, 2024
1 parent 45a88ba commit f1373f9
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 94 deletions.
19 changes: 2 additions & 17 deletions fuzz/C++/fuzz_XMLProfiles/fuzz_XMLProfiles.cxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <fastrtps/Domain.h>
#include <fastrtps/xmlparser/XMLProfileManager.h>
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>

#include "fuzz_utils.h"

Expand All @@ -23,21 +22,7 @@ extern "C" int LLVMFuzzerTestOneInput(
return EXIT_FAILURE;
}

const char* filename = buf_to_file(data, size);

if (filename == NULL)
{
return EXIT_FAILURE;
}

// TODO change this to a func. taking buf + len (or C string)
// to avoid using `buf_to_file`
xmlparser::XMLProfileManager::loadXMLFile(filename);

if (delete_file(filename) != 0)
{
return EXIT_FAILURE;
}
fastdds::dds::DomainParticipantFactory::get_instance()->load_XML_profiles_string(reinterpret_cast<const char*>(data), size);

return 0;
}
61 changes: 0 additions & 61 deletions fuzz/C++/fuzz_XMLProfiles/fuzz_utils.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,64 +32,3 @@ extern "C" int ignore_stdout(

return ret;
}

extern "C" int delete_file(
const char* pathname)
{
int ret = unlink(pathname);
if (ret == -1)
{
warn("failed to delete \"%s\"", pathname);
}

free((void*)pathname);

return ret;
}

extern "C" char* buf_to_file(
const uint8_t* buf,
size_t size)
{
char* pathname = strdup("/dev/shm/fuzz-XXXXXX");
if (pathname == NULL)
{
return NULL;
}

int fd = mkstemp(pathname);
if (fd == -1)
{
warn("mkstemp(\"%s\")", pathname);
free(pathname);
return NULL;
}

size_t pos = 0;
while (pos < size)
{
int nbytes = write(fd, &buf[pos], size - pos);
if (nbytes <= 0)
{
if (nbytes == -1 && errno == EINTR)
{
continue;
}
warn("write");
goto err;
}
pos += nbytes;
}

if (close(fd) == -1)
{
warn("close");
goto err;
}

return pathname;

err:
delete_file(pathname);
return NULL;
}
16 changes: 0 additions & 16 deletions fuzz/C++/fuzz_XMLProfiles/fuzz_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,5 @@
extern "C" int ignore_stdout(
void);

// Delete the file passed as argument and free the associated buffer. This
// function is meant to be called on buf_to_file return value.
//
// Return 0 on success, -1 otherwise.
extern "C" int delete_file(
const char* pathname);

// Write the data provided in buf to a new temporary file. This function is
// meant to be called by LLVMFuzzerTestOneInput() for fuzz targets that only
// take file names (and not data) as input.
//
// Return the path of the newly created file or NULL on error. The caller should
// eventually free the returned buffer (see delete_file).
extern "C" char* buf_to_file(
const uint8_t* buf,
size_t size);

#endif // FUZZ_UTILS_H_

0 comments on commit f1373f9

Please sign in to comment.