Skip to content

Commit

Permalink
Merge pull request #2089 from d70-t/open_mem_truncated_file
Browse files Browse the repository at this point in the history
Don't assert if trying to open truncated file from memory.
  • Loading branch information
WardF authored Mar 14, 2022
2 parents f121e0b + d281be2 commit 8152a51
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 16 deletions.
4 changes: 2 additions & 2 deletions libhdf5/hdf5file.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "ncrc.h"
#include "ncauth.h"

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

static void dumpopenobjects(NC_FILE_INFO_T* h5);

Expand Down Expand Up @@ -244,7 +244,7 @@ nc4_close_netcdf4_file(NC_FILE_INFO_T *h5, int abort, NC_memio *memio)
if (h5->mem.inmemory)
{
/* Pull out the final memory */
(void)NC4_extract_file_image(h5);
(void)NC4_extract_file_image(h5, abort);
if (!abort && memio != NULL)
{
*memio = h5->mem.memio; /* capture it */
Expand Down
29 changes: 16 additions & 13 deletions libhdf5/nc4memcb.c
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,10 @@ NC4_image_init(NC_FILE_INFO_T* h5)
if (H5Pset_file_image(fapl, udata->app_image_ptr, udata->app_image_size) < 0)
goto out;

/* Maintain a backward link */
h5->mem.udata = (void*)udata;
udata = NULL;

/* define a unique file name */
snprintf(file_name, (sizeof(file_name) - 1), "file_image_%ld", file_name_counter++);

Expand All @@ -814,10 +818,6 @@ NC4_image_init(NC_FILE_INFO_T* h5)
goto out;
}

/* Maintain a backward link */
h5->mem.udata = (void*)udata;
udata = NULL;

done:
/* Reclaim the fapl object */
H5E_BEGIN_TRY {
Expand Down Expand Up @@ -854,22 +854,25 @@ NC4_image_finalize(void* _udata)
}

int
NC4_extract_file_image(NC_FILE_INFO_T* h5)
NC4_extract_file_image(NC_FILE_INFO_T* h5, int abort)
{
int stat = NC_NOERR;
H5LT_file_image_ud_t *udata;

udata = (H5LT_file_image_ud_t *)h5->mem.udata;
assert(udata != NULL);
if(abort && udata == NULL) {
stat = NC_EHDFERR;
} else {
assert(udata != NULL);

/* Fill in h5->mem.memio from udata */
h5->mem.memio.memory = udata->vfd_image_ptr;
h5->mem.memio.size = udata->vfd_image_size;
/* Fill in h5->mem.memio from udata */
h5->mem.memio.memory = udata->vfd_image_ptr;
h5->mem.memio.size = udata->vfd_image_size;

/* Move control */
udata->vfd_image_ptr = NULL;
udata->vfd_image_size = 0;

/* Move control */
udata->vfd_image_ptr = NULL;
udata->vfd_image_size = 0;
}
return stat;
}

Expand Down
3 changes: 2 additions & 1 deletion nc_test4/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ SET(NC4_TESTS tst_dims tst_dims2 tst_dims3 tst_files tst_files4
tst_files6 tst_sync tst_h_strbug tst_h_refs tst_h_scalar tst_rename
tst_rename2 tst_rename3 tst_h5_endians tst_atts_string_rewrite tst_put_vars_two_unlim_dim
tst_hdf5_file_compat tst_fill_attr_vanish tst_rehash tst_types tst_bug324
tst_atts3 tst_put_vars tst_elatefill tst_udf tst_bug1442 tst_quantize)
tst_atts3 tst_put_vars tst_elatefill tst_udf tst_bug1442 tst_broken_files
tst_quantize)

IF(HAS_PAR_FILTERS)
SET(NC4_tests $NC4_TESTS tst_alignment)
Expand Down
41 changes: 41 additions & 0 deletions nc_test4/tst_broken_files.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* This is part of the netCDF package.
Copyright 2018 University Corporation for Atmospheric Research/Unidata
See COPYRIGHT file for conditions of use.
Test that netCDF provides proper error messages if broken Files are supplied.
*/

#include <config.h>
#include <stdio.h>
#include <nc_tests.h>
#include "err_macros.h"
#include "netcdf.h"
#include "netcdf_mem.h"

#include <string.h>

#define FILE_NAME "tst_broken_files.nc"
#define TRUNCATED_FILE_CONTENT "\x89HDF\r\n\x1a\n"

int
main() {
printf("\n*** Testing NetCDF-4 with truncated (broken) sample file.\n");
{
printf("*** testing via file on file-system ...\n");
FILE *fp = fopen(FILE_NAME, "w");
if(!fp) ERR;
if(fwrite(TRUNCATED_FILE_CONTENT, sizeof(char), sizeof(TRUNCATED_FILE_CONTENT), fp) != sizeof(TRUNCATED_FILE_CONTENT)) ERR;
fclose(fp);

int ncid;
if (nc_open(FILE_NAME, 0, &ncid) != NC_EHDFERR) ERR;
}

{
printf("*** testing via in-memory access ...\n");
int ncid;
if (nc_open_mem(FILE_NAME, 0, sizeof(TRUNCATED_FILE_CONTENT), TRUNCATED_FILE_CONTENT, &ncid) != NC_EHDFERR) ERR;
}
SUMMARIZE_ERR;
FINAL_RESULTS;
}

0 comments on commit 8152a51

Please sign in to comment.