[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 960926: netCDF bug: storing double in long attribute on Cray



> To: address@hidden
> cc: DIX Martin <address@hidden>
> From: Harvey DAVIES <address@hidden>
> Subject: netCDF bug report
> Organization: CSIRO
> Keywords: 199609260406.AA01035
>
> Using netCDF 2.4.3 on Cray Y-MP4E/464 running UNICOS 8.0.4 I get following
> problem (which does not occur on SGI system).  I have not tried netCDF 3.
>
> It seems that ncattput (which changes type from long to double) overwrites
> variable.  Here is a program which illustrates the bug:
>
> $ cat t.c
> #include "netcdf.h"
>
> int
> main() {
>
>    int  ncid;                   /* netCDF id */
>    int  year_id;                  /* variable id */
>    double  av = 6;    /* attribute value */
>
>    ncid = ncopen("m.nc", NC_WRITE);
>    year_id = ncvarid(ncid, "year");
>    ncattput (ncid, year_id, "valid_min", NC_DOUBLE, 1, (void *) &av);
>    ncclose (ncid);
>    return 0;
> }
> This following runs the program with before & after ncdumps.
> Note how the value of year changes from 9 to 92.
>
> $ ncdump m.nc
> netcdf m {
> variables:
>         long year ;
>                 year:valid_min = 6 ;
>
> data:
>
>  year = 9 ;
> }
> $ t
> $ ncdump m.nc
> netcdf m {
> variables:
>         long year ;
>                 year:valid_min = 6. ;
>
> data:
>
>  year = 92 ;
> }


Given that the valid_min attribute is previously defined as
NC_LONG, the program t.c should fail with a "Not in define mode"
error.

With netcdf 3, the output looks like this:
(shavano) 35 % a.out
ncattput: ncid 164770: Operation not allowed in data mode

And, with netcdf-2, it _should_ look something like:
ncattput: m.nc: Can't increase size unless in define mode

It turns out that the check to see whether the external space required
is increasing was munged in netcdf-2.
At the end of this file, find a patch to netcdf-2 to fix this,
or, you can build netcdf-3.1, add jackets.o from netcdf-2 for fortran
support, and start enjoying the benefits of netcdf-3 immediately.

-glenn

Index: netcdf-2.4.3/src/libsrc/array.c
===================================================================
RCS file: /upc/share/CVS/netcdf/libsrc/array.c,v
retrieving revision 1.55
diff -c -r1.55 array.c
*** array.c     1996/05/02 15:40:54     1.55
--- array.c     1996/10/02 21:46:37
***************
*** 281,286 ****
--- 281,301 ----
        memlen = count * szof ;
        if(memlen > old->count * old->szof )
                return(NULL) ; /* punt */
+
+       /* test external space as well */
+       {
+               NC_array ta;
+               ta.count = count ;
+               ta.type = type ;
+               ta.szof = szof ;
+               {
+                       int new_xlen = NC_xlen_array(&ta);
+                       int old_xlen = NC_xlen_array(old);
+                       if(new_xlen > old_xlen)
+                               return(NULL) ; /* punt */
+               }
+       }
+
        old->count = count ;
        old->type = type ;
        old->szof = szof ;