Skip to content

signal errors while getting file from JAR using optional #2154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions src/java_bytecode/jar_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,19 @@ jar_filet &jar_filet::operator=(jar_filet &&other)
return *this;
}

std::string jar_filet::get_entry(const std::string &name)
optionalt<std::string> jar_filet::get_entry(const std::string &name)
{
const auto entry=m_name_to_index.find(name);
INVARIANT(entry!=m_name_to_index.end(), "File doesn't exist");
if(entry==m_name_to_index.end())
return {};

try
{
return m_zip_archive.extract(entry->second);
}
catch(const std::runtime_error &)
{
return "";
return {};
}
}

Expand All @@ -96,21 +98,25 @@ static std::string trim(

std::unordered_map<std::string, std::string> jar_filet::get_manifest()
{
const auto entry=get_entry("META-INF/MANIFEST.MF");

if(!entry.has_value())
return {};

std::unordered_map<std::string, std::string> out;
const auto entry=m_name_to_index.find("META-INF/MANIFEST.MF");
if(entry!=m_name_to_index.end())
std::istringstream in(*entry);
std::string line;
while(std::getline(in, line))
{
std::istringstream in(this->get_entry(entry->first));
std::string line;
while(std::getline(in, line))
const auto key_end=std::find(line.cbegin(), line.cend(), ':');
if(key_end!=line.cend())
{
const auto key_end=std::find(line.cbegin(), line.cend(), ':');
if(key_end!=line.cend())
out.emplace(
trim(line.cbegin(), key_end),
trim(std::next(key_end), line.cend()));
out.emplace(
trim(line.cbegin(), key_end),
trim(std::next(key_end), line.cend()));
}
}

return out;
}

Expand Down
11 changes: 9 additions & 2 deletions src/java_bytecode/jar_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Author: Diffblue Ltd
#include <memory>
#include <string>
#include <vector>

#include <util/optional.h>

#include "mz_zip_archive.h"

class java_class_loader_limitt;
Expand Down Expand Up @@ -41,19 +44,23 @@ class jar_filet final
~jar_filet()=default;

/// Get contents of a file in the jar archive.
/// Terminates the program if file doesn't exist
/// Returns nullopt if file doesn't exist.
/// \param filename Name of the file in the archive
std::string get_entry(const std::string &filename);
optionalt<std::string> get_entry(const std::string &filename);

/// Get contents of the Manifest file in the jar archive
std::unordered_map<std::string, std::string> get_manifest();

/// Get list of filenames in the archive
std::vector<std::string> filenames() const;

private:
/// Loads the fileindex (m_name_to_index) with a map of loaded files to
/// indices.
void initialize_file_index(java_class_loader_limitt &limit);

mz_zip_archivet m_zip_archive;

/// Map of filename to the file index in the zip archive.
std::unordered_map<std::string, size_t> m_name_to_index;
};
Expand Down
7 changes: 5 additions & 2 deletions src/java_bytecode/java_class_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,14 @@ optionalt<java_bytecode_parse_treet> java_class_loadert::get_class_from_jar(
debug()
<< "Getting class `" << class_name << "' from JAR " << jar_file << eom;

std::string data =
auto data =
jar_pool(class_loader_limit, jar_file).get_entry(jar_index_it->second);

if(!data.has_value())
return {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous behaviour here was to terminate the program and now returns an empty parse tree. This seems reasonable but I wonder if this now hides typos?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't an empty parse tree, but a nullopt one -- very different from an empty string!

Note that the sole user checks existence of the file before calling, i.e., this should be unreachable.


java_bytecode_parse_treet parse_tree;
std::istringstream istream(data);
std::istringstream istream(*data);
if(java_bytecode_parse(istream, parse_tree, get_message_handler()))
return {};
return parse_tree;
Expand Down