Current-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: gcc 7 warning



On Tue, Feb 12, 2019 at 04:35:02PM +0000, Patrick Welche wrote:
> Bemused by:
> 
> xenpmd.c:90:36: error: '%s' directive output may be truncated writing up to 511 bytes into a region of size 271 [-Werror=format-truncation=]
>      #define BATTERY_INFO_FILE_PATH "/tmp/battery/%s/info"
>                                     ^
> xenpmd.c:113:52: note: in expansion of macro 'BATTERY_INFO_FILE_PATH'
>              snprintf(file_name, sizeof(file_name), BATTERY_INFO_FILE_PATH,
>                                                     ^~~~~~~~~~~~~~~~~~~~~~
> xenpmd.c:113:13: note: '__builtin_snprintf' output between 19 and 530 bytes into a destination of size 284
>              snprintf(file_name, sizeof(file_name), BATTERY_INFO_FILE_PATH,
>              ^
> 
> while trying to build xentools411. I thought that was the point of
> snprintf...

Yeah, but mostly gcc is right about warning, as the code in question will
not guarantee 0 termination for the resulting string.

You can quell the warning if you use the return value (e.g. to detect overflow
and truncate the string properly).

Example:

	char buf[STRSIZE];
	int cnt;

	cnt = snprintf(buf, sizeof(buf), format, a1, a2);
	if (cnt >= (int)sizeof(buf)-1)
		buf[sizeof(buf)-1] = 0;

The warning message itself is confusing, I still fail to parse it.

Martin


Home | Main Index | Thread Index | Old Index