netcdf-c: NC_DISKLESS returns garbage data for certain files

using the latest netcdf-c from master, NC_DISKLESS mode can return garbage data for certain files.

With the following file: foo.zip

and the following code:

#include <iostream>
#include <netcdf.h>
#include <netcdf_mem.h>
#include <iostream>
#include <fstream>
#include <assert.h>
#import <vector>


using namespace std;

#define    HRAPY  200
#define    HRAPX  333


static size_t value_count[] = {HRAPY, HRAPX};
static size_t start[] = {0, 0};

int main(int argc, const char * argv[]) {
    ifstream file("/tmp/foo.nc", ios::in | ios::binary | ios::ate);
    streamsize size = file.tellg();
    file.seekg(0, ios::beg);
    
    std::vector<char> buffer(size);
    int status;
    int ncid;
    
    if (file.read(buffer.data(), size))
    {
//        status = nc_open_mem(".nc", 0, size, buffer.data(), &ncid);
        status = nc_open("/tmp/foo.nc", NC_DISKLESS|NC_CDF5, &ncid);
        if(status != NC_NOERR) {
            cout << nc_strerror(status);
            assert(false);
        }
        
        int rh_id;
        status = nc_inq_varid(ncid, "amountofprecip", &rh_id);
        if(status != NC_NOERR) {
            cout << nc_strerror(status);
            assert(false);
        }
        
        int rh_vals[HRAPY][HRAPX];
        status = nc_get_vara_int(ncid, rh_id, start, value_count, (int*)rh_vals);
        if(status != NC_NOERR) {
            cout << nc_strerror(status);
            assert(false);
        }
    }

    std::cout << "Hello, World!\n";
    return 0;
}

note there’s no errors, however rows > 100 return garbage, row 100 looks like:

[ -71   76   96   37   20    8  -67  -47  -89   62   52  -52   78   86  -62
   60   46   44  -68  -44  -34   28   30  -30   -2   -1   14  -64  -68  -63
  -88  -86  -95   40  -70  -55    5  -56  -50  -61   88   57   45  -73   -3
  -45   19    5  -73   -6  -

whereas row 101 looks like:

[ 4325376  2818048   393215 -3997697 -4063233 -1114113 -1310720  4915199
 -6488064  5832703 -1114113 -2228225 -2686977 -3932160  6160384  3276799
  -720896  3670015 -4063232  6225919 -5636096  1966080  3014655 -4063233
 -5308416  1114112  3407871 -4653057 

opening the same file with nc_open does not return corrupt data.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 33 (27 by maintainers)

Commits related to this issue

Most upvoted comments

using

clang tst_diskless_mem.c -lnetcdf -o tst_diskless_mem

and running it, doesn’t seem to reproduce any garbage data.

Interestingly enough, when I try to run this test from a straight C program integrated into the unit tests, the issue disappears. Investigating further.

On Tue, May 2, 2017 at 2:17 PM, Alexander Mohr notifications@github.com wrote:

doesn’t look like it

brew install homebrew/science/netcdf Updating Homebrew… ==> Auto-updated Homebrew! Updated 1 tap (caskroom/cask). No changes to formulae.

==> Installing netcdf from homebrew/science ==> Downloading https://homebrew.bintray.com/bottles-science/netcdf-4.4.1.1_4.sierra.bottle.tar.gz Already downloaded: /Users/amohr/Library/Caches/Homebrew/netcdf-4.4.1.1_4.sierra.bottle.tar.gz ==> Pouring netcdf-4.4.1.1_4.sierra.bottle.tar.gz 🍺 /usr/local/Cellar/netcdf/4.4.1.1_4: 66 files, 2.3MB

amohr@amohr-macbook:~/dev/thehesiod/netcdf4-python/netCDF4$ rgrep /usr/local/Cellar/netcdf/4.4.1.1_4/include “HAVE_MREMAP|HAVE_MMAP” amohr@amohr-macbook:~/dev/thehesiod/netcdf4-python/netCDF4$

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Unidata/netcdf-c/issues/401#issuecomment-298748577, or mute the thread https://github.com/notifications/unsubscribe-auth/AEH-UmZovQ8b0a_Qy_61R_xbWZaaqs5Hks5r149igaJpZM4NNiV- .

Thanks; replicated on my end, and using NC_DISKLESS|NC_CDF5 does not correct the issue.

I will take a look; I was out of town for the past week but will start digging into this shortly.