Skip to content

Commit c4f1baa

Browse files
authored
Making get_corpus safer (simdjson#360)
1 parent 3439ce1 commit c4f1baa

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/jsonioutil.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "simdjson/jsonioutil.h"
22
#include <cstdlib>
33
#include <cstring>
4+
#include <climits>
45

56
namespace simdjson {
67
char *allocate_padded_buffer(size_t length) {
@@ -16,8 +17,16 @@ char *allocate_padded_buffer(size_t length) {
1617
padded_string get_corpus(const std::string &filename) {
1718
std::FILE *fp = std::fopen(filename.c_str(), "rb");
1819
if (fp != nullptr) {
19-
std::fseek(fp, 0, SEEK_END);
20-
size_t len = std::ftell(fp);
20+
if(std::fseek(fp, 0, SEEK_END) < 0) {
21+
std::fclose(fp);
22+
throw std::runtime_error("cannot seek in the file");
23+
}
24+
long llen = std::ftell(fp);
25+
if((llen < 0) || (llen == LONG_MAX)) {
26+
std::fclose(fp);
27+
throw std::runtime_error("cannot tell where we are in the file");
28+
}
29+
size_t len = (size_t) llen;
2130
padded_string s(len);
2231
if (s.data() == nullptr) {
2332
std::fclose(fp);

0 commit comments

Comments
 (0)