Menu

Bug in Mat_VarGetStructsLinear()

2012-02-03
2013-05-30
  • Takumi Kamioka

    Takumi Kamioka - 2012-02-03

    Hi,

    I  utilize matio1.3.4.
    Thanks a lot for this great library.
    I found a bug in Mat_VarGetStructsLinear().
    The function only return structures starting from the first  structure.
    I modified the function as follows (add one line):

    matvar_t *
    Mat_VarGetStructsLinear(matvar_t *matvar,int start,int stride,int edge,
        int copy_fields)
    {
        int i, I = 0, field, nfields;
        matvar_t *struct_slab, **fields;

        /* FIXME: Check allocations */
        if ( matvar == NULL || matvar->rank > 10 ) {
           struct_slab = NULL;
        } else {

            struct_slab = Mat_VarDuplicate(matvar,0);
            if ( !copy_fields )
                struct_slab->mem_conserve = 1;

            nfields = matvar->nbytes / matvar->data_size;
            for ( i = 0; i < matvar->rank; i++ )
                nfields = nfields / matvar->dims_;

            struct_slab->nbytes = edge*nfields*sizeof(matvar_t *);
            struct_slab->data = malloc(struct_slab->nbytes);
            fields = struct_slab->data;
            I = start*nfields;  /* ADD this line */
            for ( i = 0; i < edge; i++ ) {
                if ( copy_fields ) {
                    for ( field = 0; field < nfields; field++ ) {
                        fields =
                            Mat_VarDuplicate(*((matvar_t **)matvar->data+I),1);
                        I++;
                    }
                } else {
                    for ( field = 0; field < nfields; field++ ) {
                        fields = *((matvar_t **)matvar->data + I);
                        I++;
                    }
                }
                I += stride;
            }
        }
        return struct_slab;
    }

    That works fine for my usage.
    Thanks,_

     
  • cch

    cch - 2012-02-07

    Thanks!

    Chris

     
  • Takumi Kamioka

    Takumi Kamioka - 2012-02-08

    Hi, Chris!

    I found an another bug in the same function. Multiple fields could not be read from a structure obtained by this function. Because the number of fields was not set correctly.
    I modified it as follows. It's working but It might be the wrong modification.

    @@ -1291,8 +1291,9 @@ Mat_VarGetStructsLinear(matvar_t *matvar,int start,int str

             struct_slab->nbytes = edge*nfields*sizeof(matvar_t *);
             struct_slab->data = malloc(struct_slab->nbytes);
              fields = struct_slab->data;
    +        struct_slab->dims = edge;   
             I = start*nfields;
             for ( i = 0; i < edge; i++ ) {
                 if ( copy_fields ) {
                     for ( field = 0; field < nfields; field++ ) {
    @@ -1309,6 +1310,7 @@ Mat_VarGetStructsLinear(matvar_t *matvar,int start,int str
                 I += stride;
             }
         }
         return struct_slab;
    }

     

Log in to post a comment.