Fixed cnpy coverity issues (#8515)

This commit is contained in:
Anton Romanov authored and GitHub committed 2021-11-15 17:24:48 +03:00
1 parent 3b34f09a9b
commit cac45e4f29
2 files changed
+9 -7

No files matched your search

+7 -6
View File
@@ -170,7 +170,8 @@ void cnpy::parse_npy_header(FILE* fp, size_t& word_size, std::vector<size_t>& sh
void cnpy::parse_zip_footer(FILE* fp, uint16_t& nrecs, size_t& global_header_size, size_t& global_header_offset)
{
std::vector<char> footer(22);
fseek(fp,-22,SEEK_END);
if(fseek(fp,-22,SEEK_END) != 0)
throw std::runtime_error("parse_zip_footer: failed fseek");
size_t res = fread(&footer[0],sizeof(char),22,fp);
if(res != 22)
throw std::runtime_error("parse_zip_footer: failed fread");
@@ -195,7 +196,7 @@ cnpy::NpyArray load_the_npy_file(FILE* fp) {
size_t word_size = 0;
bool fortran_order = false;
cnpy::parse_npy_header(fp,word_size,shape,fortran_order);
if (word_size >= 0 && word_size < ULLONG_MAX) {
if (word_size > 0 && word_size < ULLONG_MAX) {
cnpy::NpyArray arr(shape, word_size, fortran_order);
size_t nread = fread(arr.data<char>(), 1, arr.num_bytes(), fp);
if (nread != arr.num_bytes())
@@ -223,6 +224,7 @@ cnpy::NpyArray load_the_npz_array(FILE* fp, uint32_t compr_bytes, uint32_t uncom
d_stream.opaque = Z_NULL;
d_stream.avail_in = 0;
d_stream.next_in = Z_NULL;
d_stream.total_out = 0;
err = inflateInit2(&d_stream, -MAX_WBITS);
d_stream.avail_in = compr_bytes;
@@ -237,7 +239,7 @@ cnpy::NpyArray load_the_npz_array(FILE* fp, uint32_t compr_bytes, uint32_t uncom
size_t word_size = 0;
bool fortran_order = false;
cnpy::parse_npy_header(&buffer_uncompr[0],word_size,shape,fortran_order);
if (word_size >= 0 && word_size < ULLONG_MAX) {
if (word_size > 0 && word_size < ULLONG_MAX) {
cnpy::NpyArray array(shape, word_size, fortran_order);
size_t offset = uncompr_bytes - array.num_bytes();
@@ -344,7 +346,8 @@ cnpy::NpyArray cnpy::npz_load(std::string fname, std::string varname) {
else {
//skip past the data
uint32_t size = *(uint32_t*) &local_header[22];
fseek(fp,size,SEEK_CUR);
if (fseek(fp,size,SEEK_CUR) !=0)
throw std::runtime_error("npz_load: failed fseek");
}
}
@@ -370,5 +373,3 @@ cnpy::NpyArray cnpy::npy_load(std::string fname) {
}
}
+2 -1
View File
@@ -155,7 +155,8 @@ namespace cnpy {
//below, we will write the the new data at the start of the global header then append the global header and footer below it
size_t global_header_size;
parse_zip_footer(fp,nrecs,global_header_size,global_header_offset);
fseek(fp,global_header_offset,SEEK_SET);
if(fseek(fp,global_header_offset,SEEK_SET)!=0 )
throw std::runtime_error("npz_save: failed fseek");
global_header.resize(global_header_size);
size_t res = fread(&global_header[0],sizeof(char),global_header_size,fp);
if(res != global_header_size){