summaryrefslogtreecommitdiffstats
path: root/hl
diff options
context:
space:
mode:
authorVailin Choi <vchoi@hdfgroup.org>2013-07-30 21:41:23 (GMT)
committerVailin Choi <vchoi@hdfgroup.org>2013-07-30 21:41:23 (GMT)
commit147522bb6118ec82f087208d5caa046f57ff801d (patch)
treefdc57bdc084da094731012092ba137336f75baf3 /hl
parent4d080e50f6bde9746d2dc12ced01d7f5758965cb (diff)
downloadhdf5-147522bb6118ec82f087208d5caa046f57ff801d.zip
hdf5-147522bb6118ec82f087208d5caa046f57ff801d.tar.gz
hdf5-147522bb6118ec82f087208d5caa046f57ff801d.tar.bz2
[svn-r23948] Bring revisions #23670 - 23713 from trunk to revise_chunks.
h5committested.
Diffstat (limited to 'hl')
-rw-r--r--hl/c++/src/Makefile.in2
-rw-r--r--hl/fortran/src/Makefile.in2
-rw-r--r--hl/src/Makefile.in2
-rw-r--r--hl/test/test_image.c186
4 files changed, 135 insertions, 57 deletions
diff --git a/hl/c++/src/Makefile.in b/hl/c++/src/Makefile.in
index 717e0ae..2abf918 100644
--- a/hl/c++/src/Makefile.in
+++ b/hl/c++/src/Makefile.in
@@ -458,7 +458,7 @@ CHECK_CLEANFILES = *.chkexe *.chklog *.clog
# Add libtool shared library version numbers to the HDF5 library
# See libtool versioning documentation online.
LT_VERS_INTERFACE = 6
-LT_VERS_REVISION = 141
+LT_VERS_REVISION = 142
LT_VERS_AGE = 0
# Include src directory
diff --git a/hl/fortran/src/Makefile.in b/hl/fortran/src/Makefile.in
index ff77de7..4e00046 100644
--- a/hl/fortran/src/Makefile.in
+++ b/hl/fortran/src/Makefile.in
@@ -474,7 +474,7 @@ CHECK_CLEANFILES = *.chkexe *.chklog *.clog
# Add libtool shared library version numbers to the HDF5 library
# See libtool versioning documentation online.
LT_VERS_INTERFACE = 6
-LT_VERS_REVISION = 141
+LT_VERS_REVISION = 142
LT_VERS_AGE = 0
INCLUDES = -I$(top_srcdir)/src -I$(top_srcdir)/hl/src -I$(top_builddir)/hl/src \
-I$(top_srcdir)/fortran/src -I$(top_builddir)/fortran/src
diff --git a/hl/src/Makefile.in b/hl/src/Makefile.in
index a2a47ad..409538e 100644
--- a/hl/src/Makefile.in
+++ b/hl/src/Makefile.in
@@ -457,7 +457,7 @@ CHECK_CLEANFILES = *.chkexe *.chklog *.clog
# Add libtool shared library version numbers to the HDF5 library
# See libtool versioning documentation online.
LT_VERS_INTERFACE = 6
-LT_VERS_REVISION = 141
+LT_VERS_REVISION = 142
LT_VERS_AGE = 0
# This library is our main target.
diff --git a/hl/test/test_image.c b/hl/test/test_image.c
index a4a10e4..38f1830 100644
--- a/hl/test/test_image.c
+++ b/hl/test/test_image.c
@@ -100,24 +100,34 @@ static int test_simple(void)
hssize_t npals;
/* 8-bit image */
- unsigned char buf1 [ WIDTH*HEIGHT ];
+ unsigned char *buf1 = NULL;
unsigned char pal[ PAL_ENTRIES * 3 ]; /* palette array */
hsize_t pal_dims[2] = {PAL_ENTRIES,3}; /* palette dimensions */
/* 24-bit image */
- unsigned char buf2 [ WIDTH*HEIGHT*3 ];
+ unsigned char *buf2 = NULL;
/* read data */
- unsigned char buf1_out [ WIDTH*HEIGHT ];
- unsigned char buf2_out [ WIDTH*HEIGHT*3 ];
+ unsigned char *buf1_out = NULL;
+ unsigned char *buf2_out = NULL;
unsigned char pal_out[ PAL_ENTRIES * 3 ]; /* palette array */
hsize_t pal_dims_out[2]; /* palette dimensions */
+ /* Allocate image buffers */
+ buf1 = (unsigned char *)HDmalloc(WIDTH * HEIGHT);
+ HDassert(buf1);
+ buf2 = (unsigned char *)HDmalloc(WIDTH * HEIGHT * 3);
+ HDassert(buf2);
+ buf1_out = (unsigned char *)HDmalloc(WIDTH * HEIGHT);
+ HDassert(buf1_out);
+ buf2_out = (unsigned char *)HDmalloc(WIDTH * HEIGHT * 3);
+ HDassert(buf2_out);
+
/* create an image */
space = WIDTH*HEIGHT / PAL_ENTRIES;
for (i=0, j=0, n=0; i < WIDTH*HEIGHT; i++, j++ )
{
- buf1[i] = n;
+ buf1[i] = (unsigned char)n;
if ( j > space )
{
n++;
@@ -126,17 +136,13 @@ static int test_simple(void)
}
-
/* create an image */
space = WIDTH*HEIGHT / 256;
for (i=0, j=0, n=0; i < WIDTH*HEIGHT*3; i+=3, j++ )
{
- unsigned char r, g, b;
-
- r = n; g = 0; b = 255-n;
- buf2[i] = r;
- buf2[i+1] = g;
- buf2[i+2] = b;
+ buf2[i] = (unsigned char)n;
+ buf2[i+1] = 0;
+ buf2[i+2] = (unsigned char)(255 - n);
if ( j > space )
{
n++;
@@ -150,9 +156,9 @@ static int test_simple(void)
*/
for ( i=0, n=0; i<PAL_ENTRIES*3; i+=3, n++)
{
- pal[i] =n; /* red */
+ pal[i] =(unsigned char)n; /* red */
pal[i+1]=0; /* green */
- pal[i+2]=255-n; /* blue */
+ pal[i+2]=(unsigned char)(255 - n); /* blue */
}
/* Create a new HDF5 file using default properties. */
@@ -283,6 +289,15 @@ static int test_simple(void)
*-------------------------------------------------------------------------
*/
+ if(buf1)
+ HDfree(buf1);
+ if(buf2)
+ HDfree(buf2);
+ if(buf1_out)
+ HDfree(buf1_out);
+ if(buf2_out)
+ HDfree(buf2_out);
+
/* Close the file. */
if(H5Fclose( fid ) < 0)
goto out;
@@ -294,6 +309,14 @@ static int test_simple(void)
/* error zone, gracefully close */
out:
+ if(buf1)
+ HDfree(buf1);
+ if(buf2)
+ HDfree(buf2);
+ if(buf1_out)
+ HDfree(buf1_out);
+ if(buf2_out)
+ HDfree(buf2_out);
H5E_BEGIN_TRY {
H5Fclose(fid);
} H5E_END_TRY;
@@ -367,7 +390,7 @@ static int test_data(void)
*/
/* read a PAL file */
- if (read_palette(PAL2_FILE, rgb, sizeof(rgb))<0)
+ if (read_palette(PAL2_FILE, rgb, (sizeof(rgb) / sizeof(rgb[0]))) < 0)
goto out;
/* transfer to the HDF5 buffer */
@@ -393,7 +416,7 @@ static int test_data(void)
*/
/* read a PAL file */
- if (read_palette(PAL3_FILE, rgb, sizeof(rgb))<0)
+ if (read_palette(PAL3_FILE, rgb, (sizeof(rgb) / sizeof(rgb[0]))) < 0)
goto out;
/* transfer to the HDF5 buffer */
@@ -422,9 +445,9 @@ static int test_data(void)
*/
for ( i=0, n=0; i<256*3; i+=3, n++)
{
- pal[i] =n;
+ pal[i] =(unsigned char)n;
pal[i+1]=0;
- pal[i+2]=255-n;
+ pal[i+2]=(unsigned char)(255 - n);
}
/* make a palette */
@@ -478,13 +501,21 @@ static int test_data(void)
if (H5Fclose(fid)<0)
goto out;
+ /* Release memory buffer */
+ HDfree(image_data);
+
return 0;
/* error zone, gracefully close */
out:
+ /* Release memory buffer */
+ if(image_data)
+ HDfree(image_data);
+
H5E_BEGIN_TRY {
H5Fclose(fid);
} H5E_END_TRY;
+
H5_FAILED();
return FAIL;
}
@@ -509,6 +540,7 @@ static int test_generate(void)
hsize_t pal_dims[2] = { 256, 3 };
float *data;
int imax, jmax, kmax;
+ int n_elements;
float valex, xmin, xmax, value;
FILE *f;
const char *data_file = H5_get_srcdir_filename(DATA_FILE4);
@@ -530,8 +562,7 @@ static int test_generate(void)
if ( f == NULL )
{
printf( "Could not find file %s. Try set $srcdir \n", data_file );
- H5Fclose(fid);
- return -1;
+ goto out;
}
/*
@@ -573,10 +604,30 @@ static int test_generate(void)
fscanf( f, "%d %d %d", &imax, &jmax, &kmax );
fscanf( f, "%f %f %f", &valex, &xmin, &xmax );
- data = (float*) HDmalloc ( imax * jmax * kmax * sizeof( float ));
- image_data = (unsigned char*) HDmalloc ( imax * jmax * kmax * sizeof( unsigned char ));
+ /* Sanity check on scanned-in values */
+ if(imax < 1 || jmax < 1 || kmax < 1)
+ goto out;
- for ( i = 0; i < imax * jmax * kmax; i++ )
+ /* Test product for integer overflow */
+ if(imax > INT_MAX / jmax)
+ goto out;
+ if(imax * jmax > INT_MAX / kmax)
+ goto out;
+
+ n_elements = imax * jmax * kmax;
+
+ /* Test buffer sizes for overflow */
+ if(n_elements > INT_MAX / (int)sizeof(unsigned char))
+ goto out;
+ if(n_elements > INT_MAX / (int)sizeof(float))
+ goto out;
+
+ data = (float *)HDmalloc((size_t)n_elements * sizeof( float ));
+ HDassert(data);
+ image_data = (unsigned char *)HDmalloc((size_t)n_elements * sizeof( unsigned char ));
+ HDassert(image_data);
+
+ for ( i = 0; i < n_elements; i++ )
{
fscanf( f, "%f ", &value );
data[i] = value;
@@ -591,10 +642,8 @@ static int test_generate(void)
TESTING2("make indexed image from all the data");
- for ( i = 0; i < imax * jmax * kmax; i++ )
- {
+ for ( i = 0; i < n_elements; i++ )
image_data[i] = (unsigned char)(( 255 * (data[i] - xmin ) ) / (xmax - xmin ));
- }
/* Make the image */
if ((H5IMmake_image_8bit(fid,"All data",(hsize_t)imax,(hsize_t)jmax,image_data))<0)
@@ -610,7 +659,7 @@ static int test_generate(void)
TESTING2("make indexed image from land data");
- for ( i = 0; i < imax * jmax * kmax; i++ )
+ for ( i = 0; i < n_elements; i++ )
{
if ( data[i] < 0 )
image_data[i] = 0;
@@ -632,7 +681,7 @@ static int test_generate(void)
TESTING2("make indexed image from sea data");
- for ( i = 0; i < imax * jmax * kmax; i++ )
+ for ( i = 0; i < n_elements; i++ )
{
if ( data[i] > 0 )
image_data[i] = 0;
@@ -675,12 +724,21 @@ static int test_generate(void)
if (H5Fclose(fid)<0)
goto out;
+ /* Release memory buffers */
+ HDfree(data);
+ HDfree(image_data);
+
/* Indicate success */
return 0;
/* error zone, gracefully close */
out:
- HDfree(data);
+ /* Release memory buffers */
+ if(data)
+ HDfree(data);
+ if(image_data)
+ HDfree(image_data);
+
H5E_BEGIN_TRY {
H5Fclose(fid);
} H5E_END_TRY;
@@ -713,51 +771,71 @@ static int read_data( const char* fname, /*IN*/
int i, n;
int color_planes;
char str[20];
- FILE *f;
+ FILE *f = NULL;
int w, h;
+ int n_elements;
const char *data_file = H5_get_srcdir_filename(fname);
+ int ret_val = -1;
/*-------------------------------------------------------------------------
* read
*-------------------------------------------------------------------------
*/
- f = HDfopen(data_file, "r");
- if ( f == NULL )
- {
+ if(NULL == (f = HDfopen(data_file, "r"))) {
printf( "Could not open file %s. Try set $srcdir \n", data_file );
- return -1;
+ goto out;
}
- fscanf( f, "%s", str );
- fscanf( f, "%d", &color_planes );
- fscanf( f, "%s", str );
- fscanf( f, "%d", &h);
- fscanf( f, "%s", str );
- fscanf( f, "%d", &w);
+ fscanf(f, "%s", str);
+ fscanf(f, "%d", &color_planes);
+ fscanf(f, "%s", str);
+ fscanf(f, "%d", &h);
+ fscanf(f, "%s", str);
+ fscanf(f, "%d", &w);
*width = (hsize_t)w;
*height = (hsize_t)h;
- if ( image_data )
- {
- HDfree( image_data );
- image_data=NULL;
- }
+ /* Check product for overflow */
+ if(w < 1 || h < 1 || color_planes < 1)
+ goto out;
+ if(w > INT_MAX / h)
+ goto out;
+ if(w * h > INT_MAX / color_planes)
+ goto out;
- image_data = (unsigned char*) HDmalloc (w * h * color_planes * sizeof( unsigned char ));
+ /* Compute buffer size */
+ n_elements = w * h * color_planes;
- for (i = 0; i < h * w * color_planes ; i++)
- {
- fscanf( f, "%d",&n );
+ /* Check buffer size for overflow */
+ if(n_elements > INT_MAX / (int)sizeof(unsigned char))
+ goto out;
+
+ /* Release the buffer, if it was previously allocated */
+ if(image_data) {
+ HDfree(image_data);
+ image_data = NULL;
+ } /* end if */
+
+ /* Allocate the image data buffer */
+ image_data = (unsigned char *)HDmalloc((size_t)n_elements * sizeof(unsigned char));
+
+ /* Read data elements */
+ for(i = 0; i < n_elements; i++) {
+ fscanf(f, "%d",&n);
image_data[i] = (unsigned char)n;
- }
- HDfclose(f);
+ } /* end for */
- return 1;
+ /* Indicate success */
+ ret_val = 1;
-}
+out:
+ if(f)
+ HDfclose(f);
+ return ret_val;
+} /* end read_data() */
/*-------------------------------------------------------------------------
@@ -882,6 +960,6 @@ static int read_palette(const char* fname,
/* close file */
HDfclose(file);
- return nentries;
+ return (int)nentries;
}