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

[no subject]



Yu-Long,

>      Following yesterday's question, though I could run and get the
> example.nc, I did get run time error from netcdf dynamic link lib.  Then
> after I trace alll possible steps, I found out the potential reason is from
> NC_new_array function memcpy statement.
> 
>                       /* can be dangerous */
>                       (void)memcpy(ret->values, values, memlen) ;
> 
> If I comment this line out, it is no run-time error happened, otherwise I
> will get page fault.  Based on your comment, I think you know there is a
> potential problem here.  Do you have better method to solve this problem
> now?  Thank you in advance.

The reason for the comment is that memcpy(s1, s2, n) is only supposed to be
used to copy n bytes from s2 to s1 if the regions of memory from s1 and s2
are known to not overlap.  If an overlap is possible, then memcpy should be
replaced by memmove, which is supposedly slower but safer in this unlikely
case.  Since the s1 in this case is a pointer to at least n bytes of newly
allocated memory space, any overlap in this case would be a symptom of a
different problem, perhaps with malloc() or free().  This seems very
unlikely, but you could test for this possibility by replacing "memcpy" by
"memmove" for this one call.

I suspect you will still get a run-time error, probably due to the "values"
pointer passed in to NC_new_array() being a bad address.  There is a check
that this pointer isn't NULL, but it could be any other bad value outside
your memory and there is no way for the library to check this; it just gets
a run-time error when an attempt is made to access memory outside of your
process's assigned memory in this call.  To diagnose the problem better
would require a stack trace of all the nested function calls and values of
their arguments.  You could get this by compiling the library for debugging
and getting a stack trace when the error occurs.  We might be able to help
at that point ...

It would be best to send future messages for netcdf support to
"address@hidden" rather than directly to me, because that way they
will get routed to someone who is here rather than sitting in my mailbox
for a couple of weeks if I happen to be on vacation.

> NC_array *
> NC_new_array(type, count, values)
> nc_type type ;
> unsigned count ;
> const void *values ;
> {
>       NC_array *ret ;
>       size_t memlen ;
> 
>       ret = (NC_array *)malloc(sizeof(NC_array)) ;
>       if( ret == NULL )
>               goto alloc_err ;
> 
>       ret->type = type ;
>       ret->szof = NC_typelen(type) ;
>       ret->count = count ;
>       memlen = count * ret->szof ;
>       ret->len = count * NC_xtypelen(type) ;
>       if( count != 0 )
>       {
>               ret->values = (Void*)malloc(memlen) ;
>               if(ret->values == NULL)
>                       goto alloc_err ;
>               if( values == NULL )
>               {
>                       NC_arrayfill(ret->values, memlen, type) ;
>               } else {
>                       /* can be dangerous */
>                       (void)memcpy(ret->values, values, memlen) ;
>               }
>       } else {
>               ret->values = NULL ;
>       }
>               
>       return(ret) ;
> alloc_err :
>       nc_serror("NC_new_array") ;
>       if (ret != NULL)
>           Free(ret);
>       return(NULL) ;
> }

______________________________________________________________________________

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