hdf5/tools/misc/talign.c
Quincey Koziol 9431c7a97e [svn-r20536] Description:
Clean up various warnings & code formatting issues.

	Bring changes from Coverity branch to trunk:

r20085:
Purpose: Fix coverity issue 793

Description: Modified H5S_hyper_project_simple_higher() to free the entire span
list in new_space on failure.


r20091:
This is a fix for coverity bug #1683.
Changed the two printfs to use %lu (unsigned long) for printing "dset_size".


r20162:
Purpose: Fix coverity issue 785

Description: Modified H5T_enum_nameof() to free "name" on failure if it was
allocated.  Also clarified some code in H5S_hyper_rebuild_helper().


r20189:
Addressed coverity defect 783.

H5SL_new_node() in H5SL.c was failing to free space allocated in its
first alloc if the second alloc failed.  Added a call to H5FL_FREE
to address this issue.

This is purely to keep coverity happy -- if this code is ever triggered,
we have much larger problems.

Note that this fix will trigger an unused return value complaint
from coverity next week.


r20190:
Fixed Coverity issues 1561 1565 and 1678 (UNUSED_VALUES) by moving checks of return values to after the function call.


r20191:
Fixed coverity issues 643 644 and 1678 (CHECKED_RETURN).


r20232:
Addressed coverity issues 923-925.  Replaced calls to sprintf with calls
to HDsnprintf.


r20233:
Fix coverity issue 662.  Don't try to sort 0 attributes in H5Aint.c.


r20234:
Fix coverity issue 664.  Check for NULL before dereferencing in H5Gdeprec.c.


r20271:
Purpose: Fix coverity issue 784

Description: Modified H5_debug_mask() to keep a list of files opened for use as
a debugging output stream, and modified H5_term_library to close these files on
exit.


r20272:
addressed coverity issues 838 & 955.  Issue was use of strcpy() -- existing
code was safe, but modified to use strncpy() to keep coverity happy.


r20273:
Addresed coverity issues 1388 and 1389.
Initialized sel_iter->type to NULL in H5S_select_iter_init.


r20275:
Purpose: Fix valgrind issue in mf.c

Description: Fixed bug (incomplete if statement) in test_mf_fs_alloc_free() so
the retrieved node gets freed.


Tested on:
        FreeBSD/32 6.3 (duty) in debug mode
        FreeBSD/64 6.3 (liberty) w/C++ & FORTRAN, in debug mode
        Linux/32 2.6 (jam) w/PGI compilers, w/default API=1.8.x,
                w/C++ & FORTRAN, w/threadsafe, in debug mode
        Linux/64-amd64 2.6 (amani) w/Intel compilers, w/default API=1.6.x,
                w/C++ & FORTRAN, in production mode
        Solaris/32 2.10 (linew) w/deprecated symbols disabled, w/C++ & FORTRAN,
                w/szip filter, w/threadsafe, in production mode
        Linux/PPC 2.6 (heiwa) w/C++ & FORTRAN, w/threadsafe, in debug mode
2011-04-17 13:57:07 -05:00

236 lines
7.0 KiB
C

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the files COPYING and Copyright.html. COPYING can be found at the root *
* of the source code distribution tree; Copyright.html can be found at the *
* root level of an installed copy of the electronic HDF5 document set and *
* is linked from the top-level documents page. It can also be found at *
* http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have *
* access to either file, you may request a copy from help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* Small program to illustrate the "misalignment" of members within a compound
* datatype, in a datatype fixed by H5Tget_native_type().
*/
#include <string.h>
#include <stdlib.h>
/*#include <unistd.h> *//* Required for unlink() */
#include "hdf5.h"
#include "H5private.h"
const char *fname = "talign.h5";
const char *setname = "align";
/*
* This program assumes that there is no extra space between the members 'Ok'
* and 'Not Ok', (there shouldn't be because they are of the same atomic type
* H5T_NATIVE_FLOAT, and they are placed within the compound next to one
* another per construction)
*/
int main(void)
{
hid_t fil,spc,set;
hid_t cs6, cmp, fix;
hid_t cmp1, cmp2, cmp3;
hid_t plist;
hid_t array_dt;
hsize_t dim[2];
hsize_t cdim[4];
char string5[5];
float fok[2] = {1234., 2341.};
float fnok[2] = {5678., 6785.};
float *fptr;
char *data = NULL;
int result = 0;
herr_t error = 1;
printf("%-70s", "Testing alignment in compound datatypes");
strcpy(string5, "Hi!");
HDunlink(fname);
fil = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
if (fil < 0) {
puts("*FAILED*");
return 1;
}
H5E_BEGIN_TRY {
(void)H5Ldelete(fil, setname, H5P_DEFAULT);
} H5E_END_TRY;
cs6 = H5Tcopy(H5T_C_S1);
H5Tset_size(cs6, sizeof(string5));
H5Tset_strpad(cs6, H5T_STR_NULLPAD);
cmp = H5Tcreate(H5T_COMPOUND, sizeof(fok) + sizeof(string5) + sizeof(fnok));
H5Tinsert(cmp, "Awkward length", 0, cs6);
cdim[0] = sizeof(fok) / sizeof(float);
array_dt = H5Tarray_create2(H5T_NATIVE_FLOAT, 1, cdim);
H5Tinsert(cmp, "Ok", sizeof(string5), array_dt);
H5Tclose(array_dt);
cdim[0] = sizeof(fnok) / sizeof(float);
array_dt = H5Tarray_create2(H5T_NATIVE_FLOAT, 1, cdim);
H5Tinsert(cmp, "Not Ok", sizeof(fok) + sizeof(string5), array_dt);
H5Tclose(array_dt);
fix = h5tools_get_native_type(cmp);
cmp1 = H5Tcreate(H5T_COMPOUND, sizeof(fok));
cdim[0] = sizeof(fok) / sizeof(float);
array_dt = H5Tarray_create2(H5T_NATIVE_FLOAT, 1, cdim);
H5Tinsert(cmp1, "Ok", 0, array_dt);
H5Tclose(array_dt);
cmp2 = H5Tcreate(H5T_COMPOUND, sizeof(string5));
H5Tinsert(cmp2, "Awkward length", 0, cs6);
cmp3 = H5Tcreate(H5T_COMPOUND, sizeof(fnok));
cdim[0] = sizeof(fnok) / sizeof(float);
array_dt = H5Tarray_create2(H5T_NATIVE_FLOAT, 1, cdim);
H5Tinsert(cmp3, "Not Ok", 0, array_dt);
H5Tclose(array_dt);
plist = H5Pcreate(H5P_DATASET_XFER);
if((error = H5Pset_preserve(plist, 1)) < 0)
goto out;
/*
* Create a small dataset, and write data into it we write each field
* in turn so that we are avoid alignment issues at this point
*/
dim[0] = 1;
spc = H5Screate_simple(1, dim, NULL);
set = H5Dcreate2(fil, setname, cmp, spc, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
H5Dwrite(set, cmp1, spc, H5S_ALL, plist, fok);
H5Dwrite(set, cmp2, spc, H5S_ALL, plist, string5);
H5Dwrite(set, cmp3, spc, H5S_ALL, plist, fnok);
H5Dclose(set);
/* Now open the set, and read it back in */
data = malloc(H5Tget_size(fix));
if(!data) {
perror("malloc() failed");
abort();
}
set = H5Dopen2(fil, setname, H5P_DEFAULT);
H5Dread(set, fix, spc, H5S_ALL, H5P_DEFAULT, data);
fptr = (float *)(data + H5Tget_member_offset(fix, 1));
out:
if(error < 0) {
result = 1;
puts("*FAILED - HDF5 library error*");
} else if(fok[0] != fptr[0] || fok[1] != fptr[1]
|| fnok[0] != fptr[2] || fnok[1] != fptr[3]) {
char *mname;
result = 1;
mname = H5Tget_member_name(fix, 0);
printf("%14s (%2d) %6s = %s\n",
mname, (int)H5Tget_member_offset(fix,0),
string5, (char *)(data + H5Tget_member_offset(fix, 0)));
free(mname);
fptr = (float *)(data + H5Tget_member_offset(fix, 1));
mname = H5Tget_member_name(fix, 1);
printf("Data comparison:\n"
"%14s (%2d) %6f = %f\n"
" %6f = %f\n",
mname, (int)H5Tget_member_offset(fix,1),
fok[0], fptr[0],
fok[1], fptr[1]);
free(mname);
fptr = (float *)(data + H5Tget_member_offset(fix, 2));
mname = H5Tget_member_name(fix, 2);
printf("%14s (%2d) %6f = %f\n"
" %6f = %6f\n",
mname, (int)H5Tget_member_offset(fix,2),
fnok[0], fptr[0],
fnok[1], fptr[1]);
free(mname);
fptr = (float *)(data + H5Tget_member_offset(fix, 1));
printf("\n"
"Short circuit\n"
" %6f = %f\n"
" %6f = %f\n"
" %6f = %f\n"
" %6f = %f\n",
fok[0], fptr[0],
fok[1], fptr[1],
fnok[0], fptr[2],
fnok[1], fptr[3]);
puts("*FAILED - compound type alignmnent problem*");
} else {
puts(" PASSED");
}
if(data)
free(data);
H5Sclose(spc);
H5Tclose(cmp);
H5Tclose(cmp1);
H5Tclose(cmp2);
H5Tclose(cmp3);
H5Pclose(plist);
H5Fclose(fil);
HDunlink(fname);
fflush(stdout);
return result;
}
/*-------------------------------------------------------------------------
* Function: h5tools_get_native_type
*
* Purpose: Wrapper around H5Tget_native_type() to work around
* Problems with bitfields.
*
* Return: Success: datatype ID
*
* Failure: FAIL
*
* Programmer: Quincey Koziol
* Tuesday, October 5, 2004
*
* Modifications:
*
*-------------------------------------------------------------------------
*/
hid_t
h5tools_get_native_type(hid_t type)
{
hid_t p_type;
H5T_class_t type_class;
type_class = H5Tget_class(type);
if(type_class==H5T_BITFIELD)
p_type=H5Tcopy(type);
else
p_type = H5Tget_native_type(type,H5T_DIR_DEFAULT);
return(p_type);
}