Fix string lengths when reading HFD5 attributes

This commit is contained in:
Paul Romano 2018-10-09 11:01:30 -05:00
parent 2d1793b7f9
commit 596e05c272
3 changed files with 8 additions and 8 deletions

View file

@ -1,9 +1,11 @@
#ifndef OPENMC_HDF5_INTERFACE_H
#define OPENMC_HDF5_INTERFACE_H
#include <algorithm> // for min
#include <array>
#include <complex>
#include <cstddef>
#include <cstring> // for strlen
#include <string>
#include <sstream>
#include <vector>
@ -199,7 +201,7 @@ read_attribute(hid_t obj_id, const char* name, std::vector<std::string>& vec)
read_attr_string(obj_id, name, n, buffer[0]);
for (int i = 0; i < m; ++i) {
vec.emplace_back(&buffer[i][0], n);
vec.emplace_back(&buffer[i][0], std::min(strlen(buffer[i]), n));
}
}

View file

@ -1362,7 +1362,7 @@ contains
! Allocate a C char array to get strings
n = attribute_typesize(obj_id, to_c_string(name))
m = size(buffer)
allocate(buffer_((n+1)*m))
allocate(buffer_(n*m))
! Read attribute
call read_attr_string_c(obj_id, to_c_string(name), n, buffer_)
@ -1371,7 +1371,7 @@ contains
do i = 1, m
buffer(i) = ''
do j = 1, n
k = (i-1)*(n+1) + j
k = (i-1)*n + j
if (buffer_(k) == C_NULL_CHAR) exit
buffer(i)(j:j) = buffer_(k)
end do

View file

@ -115,10 +115,9 @@ parse_command_line(int argc, char* argv[])
// Check what type of file this is
hid_t file_id = file_open(argv[i], 'r', true);
size_t len = attribute_typesize(file_id, "filetype");
read_attr_string(file_id, "filetype", len, buffer);
std::string filetype;
read_attribute(file_id, "filetype", filetype);
file_close(file_id);
std::string filetype {buffer};
// Set path and flag for type of run
if (filetype == "statepoint") {
@ -141,8 +140,7 @@ parse_command_line(int argc, char* argv[])
// Check file type is a source file
file_id = file_open(argv[i+1], 'r', true);
len = attribute_typesize(file_id, "filetype");
read_attr_string(file_id, "filetype", len, buffer);
read_attribute(file_id, "filetype", filetype);
file_close(file_id);
if (filetype != "source") {
std::string msg {"Second file after restart flag must be a source file"};