Skip to content

Commit bbd4253

Browse files
author
Daniel Kroening
committed
signal errors while getting file from JAR using optional
1 parent 48f1af3 commit bbd4253

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

src/java_bytecode/jar_file.cpp

+19-13
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,19 @@ jar_filet &jar_filet::operator=(jar_filet &&other)
6262
return *this;
6363
}
6464

65-
std::string jar_filet::get_entry(const std::string &name)
65+
optionalt<std::string> jar_filet::get_entry(const std::string &name)
6666
{
6767
const auto entry=m_name_to_index.find(name);
68-
INVARIANT(entry!=m_name_to_index.end(), "File doesn't exist");
68+
if(entry==m_name_to_index.end())
69+
return {};
70+
6971
try
7072
{
7173
return m_zip_archive.extract(entry->second);
7274
}
7375
catch(const std::runtime_error &)
7476
{
75-
return "";
77+
return {};
7678
}
7779
}
7880

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

9799
std::unordered_map<std::string, std::string> jar_filet::get_manifest()
98100
{
101+
const auto entry=get_entry("META-INF/MANIFEST.MF");
102+
103+
if(!entry.has_value())
104+
return {};
105+
99106
std::unordered_map<std::string, std::string> out;
100-
const auto entry=m_name_to_index.find("META-INF/MANIFEST.MF");
101-
if(entry!=m_name_to_index.end())
107+
std::istringstream in(*entry);
108+
std::string line;
109+
while(std::getline(in, line))
102110
{
103-
std::istringstream in(this->get_entry(entry->first));
104-
std::string line;
105-
while(std::getline(in, line))
111+
const auto key_end=std::find(line.cbegin(), line.cend(), ':');
112+
if(key_end!=line.cend())
106113
{
107-
const auto key_end=std::find(line.cbegin(), line.cend(), ':');
108-
if(key_end!=line.cend())
109-
out.emplace(
110-
trim(line.cbegin(), key_end),
111-
trim(std::next(key_end), line.cend()));
114+
out.emplace(
115+
trim(line.cbegin(), key_end),
116+
trim(std::next(key_end), line.cend()));
112117
}
113118
}
119+
114120
return out;
115121
}
116122

src/java_bytecode/jar_file.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Author: Diffblue Ltd
1313
#include <memory>
1414
#include <string>
1515
#include <vector>
16+
17+
#include <util/optional.h>
18+
1619
#include "mz_zip_archive.h"
1720

1821
class java_class_loader_limitt;
@@ -41,19 +44,23 @@ class jar_filet final
4144
~jar_filet()=default;
4245

4346
/// Get contents of a file in the jar archive.
44-
/// Terminates the program if file doesn't exist
47+
/// Returns nullopt if file doesn't exist.
4548
/// \param filename Name of the file in the archive
46-
std::string get_entry(const std::string &filename);
49+
optionalt<std::string> get_entry(const std::string &filename);
50+
4751
/// Get contents of the Manifest file in the jar archive
4852
std::unordered_map<std::string, std::string> get_manifest();
53+
4954
/// Get list of filenames in the archive
5055
std::vector<std::string> filenames() const;
56+
5157
private:
5258
/// Loads the fileindex (m_name_to_index) with a map of loaded files to
5359
/// indices.
5460
void initialize_file_index(java_class_loader_limitt &limit);
5561

5662
mz_zip_archivet m_zip_archive;
63+
5764
/// Map of filename to the file index in the zip archive.
5865
std::unordered_map<std::string, size_t> m_name_to_index;
5966
};

src/java_bytecode/java_class_loader.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,14 @@ optionalt<java_bytecode_parse_treet> java_class_loadert::get_class_from_jar(
9191
debug()
9292
<< "Getting class `" << class_name << "' from JAR " << jar_file << eom;
9393

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

97+
if(!data.has_value())
98+
return {};
99+
97100
java_bytecode_parse_treet parse_tree;
98-
std::istringstream istream(data);
101+
std::istringstream istream(*data);
99102
if(java_bytecode_parse(istream, parse_tree, get_message_handler()))
100103
return {};
101104
return parse_tree;

0 commit comments

Comments
 (0)