Skip to content

Commit 8152a51

Browse files
authored
Merge pull request #2089 from d70-t/open_mem_truncated_file
Don't assert if trying to open truncated file from memory.
2 parents f121e0b + d281be2 commit 8152a51

File tree

4 files changed

+61
-16
lines changed

4 files changed

+61
-16
lines changed

libhdf5/hdf5file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "ncrc.h"
1717
#include "ncauth.h"
1818

19-
extern int NC4_extract_file_image(NC_FILE_INFO_T* h5); /* In nc4memcb.c */
19+
extern int NC4_extract_file_image(NC_FILE_INFO_T* h5, int abort); /* In nc4memcb.c */
2020

2121
static void dumpopenobjects(NC_FILE_INFO_T* h5);
2222

@@ -244,7 +244,7 @@ nc4_close_netcdf4_file(NC_FILE_INFO_T *h5, int abort, NC_memio *memio)
244244
if (h5->mem.inmemory)
245245
{
246246
/* Pull out the final memory */
247-
(void)NC4_extract_file_image(h5);
247+
(void)NC4_extract_file_image(h5, abort);
248248
if (!abort && memio != NULL)
249249
{
250250
*memio = h5->mem.memio; /* capture it */

libhdf5/nc4memcb.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,10 @@ NC4_image_init(NC_FILE_INFO_T* h5)
794794
if (H5Pset_file_image(fapl, udata->app_image_ptr, udata->app_image_size) < 0)
795795
goto out;
796796

797+
/* Maintain a backward link */
798+
h5->mem.udata = (void*)udata;
799+
udata = NULL;
800+
797801
/* define a unique file name */
798802
snprintf(file_name, (sizeof(file_name) - 1), "file_image_%ld", file_name_counter++);
799803

@@ -814,10 +818,6 @@ NC4_image_init(NC_FILE_INFO_T* h5)
814818
goto out;
815819
}
816820

817-
/* Maintain a backward link */
818-
h5->mem.udata = (void*)udata;
819-
udata = NULL;
820-
821821
done:
822822
/* Reclaim the fapl object */
823823
H5E_BEGIN_TRY {
@@ -854,22 +854,25 @@ NC4_image_finalize(void* _udata)
854854
}
855855

856856
int
857-
NC4_extract_file_image(NC_FILE_INFO_T* h5)
857+
NC4_extract_file_image(NC_FILE_INFO_T* h5, int abort)
858858
{
859859
int stat = NC_NOERR;
860860
H5LT_file_image_ud_t *udata;
861861

862862
udata = (H5LT_file_image_ud_t *)h5->mem.udata;
863-
assert(udata != NULL);
863+
if(abort && udata == NULL) {
864+
stat = NC_EHDFERR;
865+
} else {
866+
assert(udata != NULL);
864867

865-
/* Fill in h5->mem.memio from udata */
866-
h5->mem.memio.memory = udata->vfd_image_ptr;
867-
h5->mem.memio.size = udata->vfd_image_size;
868+
/* Fill in h5->mem.memio from udata */
869+
h5->mem.memio.memory = udata->vfd_image_ptr;
870+
h5->mem.memio.size = udata->vfd_image_size;
868871

869-
/* Move control */
870-
udata->vfd_image_ptr = NULL;
871-
udata->vfd_image_size = 0;
872-
872+
/* Move control */
873+
udata->vfd_image_ptr = NULL;
874+
udata->vfd_image_size = 0;
875+
}
873876
return stat;
874877
}
875878

nc_test4/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ SET(NC4_TESTS tst_dims tst_dims2 tst_dims3 tst_files tst_files4
2222
tst_files6 tst_sync tst_h_strbug tst_h_refs tst_h_scalar tst_rename
2323
tst_rename2 tst_rename3 tst_h5_endians tst_atts_string_rewrite tst_put_vars_two_unlim_dim
2424
tst_hdf5_file_compat tst_fill_attr_vanish tst_rehash tst_types tst_bug324
25-
tst_atts3 tst_put_vars tst_elatefill tst_udf tst_bug1442 tst_quantize)
25+
tst_atts3 tst_put_vars tst_elatefill tst_udf tst_bug1442 tst_broken_files
26+
tst_quantize)
2627

2728
IF(HAS_PAR_FILTERS)
2829
SET(NC4_tests $NC4_TESTS tst_alignment)

nc_test4/tst_broken_files.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* This is part of the netCDF package.
2+
Copyright 2018 University Corporation for Atmospheric Research/Unidata
3+
See COPYRIGHT file for conditions of use.
4+
5+
Test that netCDF provides proper error messages if broken Files are supplied.
6+
*/
7+
8+
#include <config.h>
9+
#include <stdio.h>
10+
#include <nc_tests.h>
11+
#include "err_macros.h"
12+
#include "netcdf.h"
13+
#include "netcdf_mem.h"
14+
15+
#include <string.h>
16+
17+
#define FILE_NAME "tst_broken_files.nc"
18+
#define TRUNCATED_FILE_CONTENT "\x89HDF\r\n\x1a\n"
19+
20+
int
21+
main() {
22+
printf("\n*** Testing NetCDF-4 with truncated (broken) sample file.\n");
23+
{
24+
printf("*** testing via file on file-system ...\n");
25+
FILE *fp = fopen(FILE_NAME, "w");
26+
if(!fp) ERR;
27+
if(fwrite(TRUNCATED_FILE_CONTENT, sizeof(char), sizeof(TRUNCATED_FILE_CONTENT), fp) != sizeof(TRUNCATED_FILE_CONTENT)) ERR;
28+
fclose(fp);
29+
30+
int ncid;
31+
if (nc_open(FILE_NAME, 0, &ncid) != NC_EHDFERR) ERR;
32+
}
33+
34+
{
35+
printf("*** testing via in-memory access ...\n");
36+
int ncid;
37+
if (nc_open_mem(FILE_NAME, 0, sizeof(TRUNCATED_FILE_CONTENT), TRUNCATED_FILE_CONTENT, &ncid) != NC_EHDFERR) ERR;
38+
}
39+
SUMMARIZE_ERR;
40+
FINAL_RESULTS;
41+
}

0 commit comments

Comments
 (0)