Re: [Accel-config] [PATCH] accel-config: Fixes for pedantic compiler warnings

"King, Colin" <[email protected]> Thu, 24 Aug 2023 08:35:26 +0000
Newsgroups dev.linux.lists.accel-config
Message-ID <BY5PR11MB41021B743AAB72385C37EF738D1DA@BY5PR11MB4102.namprd11.prod.outlook.com>
The xrealloc warning is a genuine issue where realloc fails, take the examp=
le:

	ret =3D realloc(ptr, size);

if ret is NULL then the reallocation failed it is important to realize ptr =
is still valid and you end up with a leak on ptr.

The function also handles the zero size realloc with a realloc attempt and =
then another attempt with a size 1.

I suspect the function should be more like:

void *xrealloc(void *ptr, size_t size)
{
	void *ret;

	if (!size)
		size =3D 1;
	ret =3D realloc(ptr, size);
	if (!ret) {
		free(ptr);
		die("Out of memory, realloc failed");=09
	}
	return ret;
}

The free() is academic since die() terminates the code, but at least it rem=
oves any doubt from static analysis tools that ptr is not being leaked=20



-----Original Message-----
From: Thomas, Ramesh <[email protected]>=20
Sent: Thursday, August 24, 2023 3:33 AM
To: [email protected]
Cc: King, Colin <[email protected]>; Thomas, Ramesh <ramesh.thomas@intel=
.com>
Subject: [PATCH] accel-config: Fixes for pedantic compiler warnings

Debian builds enable the -Wpedantic option that gives warnings for features=
 that are supported by GCC but prohibited by other compilers.
This project is tied to the GCC compiler and uses several GCC builti-ins. P=
rovide fixes to warnings that are potential issues.
Several benighn warnings are suppressed using GCC #pragma.

Signed-off-by: Ramesh Thomas <[email protected]>

--- code cut out 8< ----



+#pragma GCC diagnostic ignored "-Wuse-after-free"
+
 void *xrealloc(void *ptr, size_t size)
 {
 	void *ret =3D realloc(ptr, size);
--
2.34.1