A bug I reported some time ago seems not to be fixed in netcdf-2.4-beta1:
The following test program stops with "ncattput: extend_array: Invalid argument"
#include <netcdf.h>
#include <stdio.h>
main()
{
int ncid;
int varid;
int dimid;
ncopts = NC_VERBOSE + NC_FATAL;
ncid = nccreate("tst.nc",NC_CLOBBER);
dimid = ncdimdef(ncid,"time",10);
varid = ncvardef (ncid, "V",NC_FLOAT,1,&dimid);
ncattput(ncid, varid, "units",NC_CHAR, strlen("m/s")+1,"m/s");
ncattdel(ncid, varid, "units");
ncclose (ncid);
ncid = ncopen("tst.nc",NC_WRITE);
ncredef(ncid);
varid = ncvarid(ncid,"V");
ncattput(ncid, varid, "units",NC_CHAR, strlen("cm/s")+1,"cm/s");
ncclose(ncid);
}
Original bug report and answer follows:
>From russ@xxxxxxxxxxxxxxxx Mon Dec 19 16:44:32 1994
To: B.Bretthauer@xxxxxx (Bernward Bretthauer)
Cc: support-netcdf@xxxxxxxxxxxxxxxx, russ@xxxxxxxxxxxxxxxx
Subject: Re: 941219: Bug in netcdf2.3.2pl2
Organization: UCAR Unidata Program
Date: Mon, 19 Dec 1994 08:43:15 -0700
From: Russ Rew <russ@xxxxxxxxxxxxxxxx>
Content-Length: 1954
>Keywords: 199412191029.AA22866
Hi Bernward,
> In netcdf2.3.2pl2 I found the following bug:
>
> If you have a variable with attributes and you do the following:
>
> 1) Delete all attributes from the variable.
> 2) Close the file without adding new attributes to this variable.
> 3) Open the file again and put it into define mode.
> 4) Add an attribute to the variable.
>
> the program now stops with "ncattput: extend_array: Invalid argument".
>
> I could fix this by changing NC_incr_array in array.c:
>
> Replace
>
> array->values = (Void*)realloc(array->values,
> (array->count +1) * array->szof) ;
>
> by
>
> if (array->values == NULL)
> {
> array->values =
> (Void*)malloc((array->count +1) * array->szof) ;
> } else {
> array->values = (Void*)realloc(array->values,
> (array->count +1) * array->szof) ;
> }
>
> But I don't know if this is save.
>
> The following program can be used to trigger the bug:
>
> #include <netcdf.h>
> #include <stdio.h>
> main()
> {
> int ncid;
> int varid;
> int dimid;
> ncopts = NC_VERBOSE + NC_FATAL;
>
> ncid = nccreate("tst.nc",NC_CLOBBER);
>
> dimid = ncdimdef(ncid,"time",10);
>
>
> varid = ncvardef (ncid, "V",NC_FLOAT,1,&dimid);
>
> ncattput(ncid, varid, "units",NC_CHAR, strlen("m/s")+1,"m/s");
>
> ncattdel(ncid, varid, "units");
>
> ncclose (ncid);
>
> ncid = ncopen("tst.nc",NC_WRITE);
>
> ncredef(ncid);
>
> varid = ncvarid(ncid,"V");
>
> ncattput(ncid, varid, "units",NC_CHAR, strlen("cm/s")+1,"cm/s");
>
> ncclose(ncid);
> }
Thanks for reporting this. Your fix looks OK to me. We will incorporate
this or a similar fix into the next release.
--
Russ Rew UCAR Unidata Program
russ@xxxxxxxxxxxxxxxx P.O. Box 3000
http://www.unidata.ucar.edu/ Boulder, CO 80307-3000