netcdf-c icon indicating copy to clipboard operation
netcdf-c copied to clipboard

Type of internal struct member causes lots of sign conversion warnings

Open ZedThree opened this issue 1 year ago • 4 comments

In current main, there is a struct which is used as a header for many other objects:

typedef struct NC_OBJ
{
    NC_SORT sort; /**< Type of object. */
    char* name;   /**< Name, assumed to be null terminated. */
    size_t id;    /**< This objects ID. */
} NC_OBJ;

Here, id is a size_t, but in many places it's used or passed to an int. In fact, a quick grep shows it's used in ~240 places and it generates ~120 warnings. Switching it to an int only generates 5 warnings.

I think it's mostly used to store and look up various object IDs which are almost always int (at least in the public API), so perhaps that makes more sense?

ZedThree avatar Oct 25 '23 16:10 ZedThree

I think this is a reasonable change. I will put up a PR for it.

DennisHeimbigner avatar Oct 25 '23 18:10 DennisHeimbigner

See PR https://github.com/Unidata/netcdf-c/pull/2781

DennisHeimbigner avatar Oct 25 '23 21:10 DennisHeimbigner

There's quite a few other internal structs with similar issues. Would it make sense to move to try and consistently use int instead of size_t everywhere? There would still be issues at boundaries with other libraries (like HDF5 and the C stdlib), but they could be handled with casts as appropriate.

ZedThree avatar Oct 26 '23 14:10 ZedThree

Replacing size_t with int across the board isn't going to work, unfortunately; even reviewing something like that would be difficult, there could be a lot of unintended consequences. Doing this on a case by case basis will be the best approach.

WardF avatar Nov 03 '23 23:11 WardF