no-OS icon indicating copy to clipboard operation
no-OS copied to clipboard

iio: Introduce iio_snprintf to harden printing to buffers

Open kylex2 opened this issue 2 years ago • 4 comments

Hi,

I have introduced iio_snprintf which should detect when buffer is too small and return error.

What do you think about it?

Quote from 'man snprintf':

The functions snprintf() and vsnprintf() do not write more than size bytes (including the terminating null byte ('\0')). If the output was truncated due to this limit then the return value is the number of characters (excluding the terminating null byte) which would have been written to the final string if enough space had been available. Thus, a return value of size or more means that the output was truncated.

Signed-off-by: Alexander Vickberg [email protected]

kylex2 avatar Mar 09 '22 16:03 kylex2

Added attribute for format checking

kylex2 avatar Mar 10 '22 07:03 kylex2

Add function description. Handle case where incoming buffer length is zero and no characters written. Remove faulty null termination.

kylex2 avatar Mar 10 '22 09:03 kylex2

Hi @kylex2 , This looks good in principle, but how about using iio_format_value ? Example usage: https://github.com/analogdevicesinc/no-OS/blob/d6b3591b5af2969818a55795681332f39ac29e1f/drivers/cdc/ad7746/iio_ad7746.c#L258

The occurrences of snprintf that you replaced with iio_snprintf should in my opinion be replaced with something like iio_format_value, this way the logic of how formatting is done is hidden from the iio driver writer and is common for all drivers.

I should say that I agree with the change myself (let's see other opinions) but if we merge this PR, then each iio_snprintf occurrence should gradually disappear in favor of iio_format_value.

buha avatar Mar 14 '22 09:03 buha

iio_format_value could make sense in many cases. I can see if I get some time to look into it, if so I can add commits to this pull request.

kylex2 avatar Mar 15 '22 08:03 kylex2

Closing this for now, might reconsider adapting/testing later on.

buha avatar May 11 '23 10:05 buha