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

[netCDF #AKD-591165]: Crash when functions are invoked in different order



Hi Chad,

> I recently began using netCDF in C. I found what may either be a bug
> or an error on my part. My objective is to load the time variable,
> its units and calendar attributes, and use UDUNITS2 to convert into
> YYYY/MM. I have attached two C files that illustrate the problem I'm
> running into. Here's the order of operations that causes my problem:
> 
> 1. open file
> 2. get time dimid
> 3. get time dim length
> 4. allocate memory for time variable
> 5. load time variable
> 6. get length of "units" attribute <--- CRASH!
> 
> I get the following output:
> *** glibc detected *** ./crash: malloc(): memory corruption (fast): 

The problem is you allocated insufficient memory in this statement:

  time = (double *)malloc(n_time);

which should instead be

  time = (double *)malloc(n_time * sizeof(double));

So when you read data into the time variable, it overwrote memory that
you hadn't allocated.

I've made that mistake enough times that I can now occasionally spot
it :-) .  Unfortunately, the netCDF library can't detenct when you're
giving it too small a buffer to hold the data ...

--Russ

Russ Rew                                         UCAR Unidata Program
address@hidden                      http://www.unidata.ucar.edu



Ticket Details
===================
Ticket ID: AKD-591165
Department: Support netCDF
Priority: Normal
Status: Closed