1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <netcdf.h>
4
5 /* When using NetCDF 4.4.1 ad prior to create a CDF-5 file and defining a small
6 * variable after a big variable (> 2^32-3 bytes), the file starting offset of
7 * the small variable (and all variables defined after the big variable) is
8 * calculated incorrectly. This test program detects this bug by checking the
9 * contents of the possible overlaps between the two variables.
10 */
11
12 #define ERR {if(err!=NC_NOERR){printf("Error at line %d in %s: %s\n", __LINE__,__FILE__, nc_strerror(err));nerrs++;}}
13
14 #define FILE_NAME "tst_cdf5_begin.nc"
15
main(int argc,char * argv[])16 int main(int argc, char *argv[])
17 {
18 int i, err, nerrs=0, ncid, dimid[2], varid[2];
19 short buf[10];
20 size_t start, count;
21
22 err = nc_create(FILE_NAME, NC_CLOBBER|NC_64BIT_DATA, &ncid); ERR;
23 err = nc_def_dim(ncid, "dim0", NC_MAX_UINT, &dimid[0]); ERR
24 err = nc_def_dim(ncid, "dim1", 10, &dimid[1]); ERR
25
26 /* define one small variable after one big variable */
27 err = nc_def_var(ncid, "var_big", NC_SHORT, 1, &dimid[0], &varid[0]); ERR
28 err = nc_def_var(ncid, "var_small", NC_SHORT, 1, &dimid[1], &varid[1]); ERR
29 err = nc_set_fill(ncid, NC_NOFILL, NULL); ERR
30 err = nc_enddef(ncid); ERR
31
32 /* write to var_big in location overlapping with var_small when using
33 * netCDF 4.4.x or prior */
34 start = NC_MAX_UINT/sizeof(short);
35 count = 10;
36 for (i=0; i<10; i++) buf[i] = i;
37 err = nc_put_vara_short(ncid, varid[0], &start, &count, buf); ERR
38
39 /* write var_small */
40 for (i=0; i<10; i++) buf[i] = -1;
41 err = nc_put_var_short(ncid, varid[1], buf); ERR
42
43 /* read back var_big and check contents */
44 for (i=0; i<10; i++) buf[i] = -1;
45 err = nc_get_vara_short(ncid, varid[0], &start, &count,buf); ERR
46 for (i=0; i<10; i++) {
47 if (buf[i] != i) {
48 printf("Error at buf[%d] expect %d but got %hd\n",i,i,buf[i]);
49 nerrs++;
50 }
51 }
52 err = nc_close(ncid); ERR
53
54 return (nerrs > 0);
55 }
56
57