Menu

fields.c:459:7: error: warning: cast to pointer from integer of different size

2018-07-02
2018-08-31
  • Václav Haisman

    Václav Haisman - 2018-07-02

    I am getting the follwing warning in hs-bibutils compilation on 64 bit Windows:

    bibutils\fields.c: In function 'fields_findv_each_add':
    C:\hs-bibutils\bibutils\fields.c:459:7: error:
         warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
           v = ( void * )( (long) n );
    

    This looks fishy. The n variable is int and that is only 32 bits wide on Windows. If any pointer gets transported through the integer it will be truncated in v variable.

     
  • Chris Putnam

    Chris Putnam - 2018-07-02

    It actually is fine, though apparently the cast should be ( long long ) under Windows, rather than ( long ), and the ( long long ) cast also works under Linux; long long is 64 bit under both architectures. This should silence the warning.

    fields_findv_each() will put pointers to str, pointers to char (C-style strings), or indicies of hits into the output void pointer list (vplist), depending on the mode that the user has chosen. In the case of storing indicies in the vplist, I'm abusing the cast system to cast the index n into a void pointer so there's a uniform output mechanism. No real pointer ever passes through n, which as you point out would cause truncation and bad things.

    To extract the integer values, the code would have to look something like:

    fields *f;
    vplist hits;
    
    vplist_init( &hits );
    status = fields_findv_each( f, LEVEL_ANY, FIELDS_POSP_FLAG, &hits, "AUTHOR" );
    if ( status==FIELDS_OK ) {
        for ( i=0; i<hits.n; ++i ) {
            n = ( int ) vplist_get( &hits, i );
            /* ... */
         }
    }
    

    I don't believe anything in bibutils uses FIELDS_POSP_FLAG, but I have other code that does.

    Thanks for the report, particularly for Windows compilers that I don't have access to.

    Chris.

     
  • Chris Putnam

    Chris Putnam - 2018-08-31

    This is now updated in version 6.7

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.