Assigning int size_t (or using it in malloc) in GCC raises the following warning:
warning: converting to 'size_t' from 'int' may change the sign of the result [-Wsign-conversion]
To solve this problem, I would like to wrap the conversion in a function that first checks the conversion is correct and then performs the listing.
This is what I have so far:
int safe_size_t_from_int(size_t *dst, int src) { if(src < 0) return SAFE_ERROR_NEGATIVE; if(SIZE_MAX < INT_MAX) { if(src > (int)SIZE_MAX) return SAFE_ERROR_OVERFLOW; } *dst = (size_t)src; return SAFE_SUCCESS; }
Did I miss something? Is there a safe conversion library that already exists?
The closest thing I can find is Microsoft Intsafe.h , but it apparently only applies to Win32 types.
EDIT Changed as a comment in chux.
source share