Small bug in ncopen error handling

I am trying to use ncopen to check whether a file is a netcdf file
or not. I was expecting it to return -1 on error and set ncerr to
NC_ENOTNC (=19). Unfortunately, there seems to be a bug, in that ncerr 
is set to NC_NOERR (=0). The workaround is to test ncerr for NC_SYSERR
instead (if there wasn't a problem opening the file, then any error
arises from the file being of the wrong type). 

The following program illustrates the problem :

---------------------------------------
#include <stdio.h>
#include <stdlib.h>

#include <netcdf.h>

int main(int argc, char *argv[])
{
   char *file;
   int cdfid;

   if (argc!=2) {
      (void) fprintf(stderr, "Wrong number of arguments.\n");
   }

   file = argv[1];

   ncopts = 0;
   cdfid = ncopen(file, NC_NOWRITE);
   (void) fprintf(stderr, "File %s: cdfid = %d, ncerr = %d.\n", 
                  file, cdfid, (int) ncerr);

   return EXIT_SUCCESS;
}
---------------------------------------

The output for a non cdf file is (the above source file, in fact) :
File test.c: cdfid = -1, ncerr = 0.

The bug seems to arise from NC_new_cdf at the point where xdr_cdf is 
called. xdr_cdf calls NCadvise if it cannot read the magic number or
if it is incorrect, setting ncerr properly. Then NC_new_cdf calls
nc_serror which resets ncerr to NC_NOERR since errno is zero.

A small bug, perhaps, but one that can lead to some perplexity.

BTW, the workaround is perhaps a better solution, since ncopen will
return NC_EXDR (without the above bug, that is) when the file is not
large enough for the magic number to be read.

-- 
            Peter Neelin (neelin@xxxxxxxxxxxxxxxxx)
            Positron Imaging Laboratories,
            Montreal Neurological Institute


  • 1993 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: