diff mbox series

testsuite: Fix leaks in tree-dynamic-object-size-0.c

Message ID 20221205142850.969850-1-siddhesh@gotplt.org
State New
Headers show
Series testsuite: Fix leaks in tree-dynamic-object-size-0.c | expand

Commit Message

Siddhesh Poyarekar Dec. 5, 2022, 2:28 p.m. UTC
In commit e5cfb9cac1d7aba9a8ea73bfe7922cfaff9d61f3 I introduced tests
for strdup and strndup with leaks.  Fix those leaks.

gcc/testsuite/ChangeLog:

	* gcc.dg/builtin-dynamic-object-size-0.c (test_strdup,
	test_strndup, test_strdup_min, test_strndup_min): Free RES
	before returning from function.
---
 .../gcc.dg/builtin-dynamic-object-size-0.c    | 20 +++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

Comments

Jeff Law Dec. 5, 2022, 4:38 p.m. UTC | #1
On 12/5/22 07:28, Siddhesh Poyarekar wrote:
> In commit e5cfb9cac1d7aba9a8ea73bfe7922cfaff9d61f3 I introduced tests
> for strdup and strndup with leaks.  Fix those leaks.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* gcc.dg/builtin-dynamic-object-size-0.c (test_strdup,
> 	test_strndup, test_strdup_min, test_strndup_min): Free RES
> 	before returning from function.
We don't generally worry about these kinds of issues in the testsuite. 
My only worry would be compromising the test.  By adding the free calls 
the compiler might match up the allocation and release and potentially 
turn it into an alloca.  I don't think we're likely to do that in this 
case, but it's worth keeping in mind.

So OK as long as you've verified the test still does what it's supposed 
to do.

jeff
Siddhesh Poyarekar Dec. 5, 2022, 4:58 p.m. UTC | #2
On 2022-12-05 11:38, Jeff Law wrote:
> 
> 
> On 12/5/22 07:28, Siddhesh Poyarekar wrote:
>> In commit e5cfb9cac1d7aba9a8ea73bfe7922cfaff9d61f3 I introduced tests
>> for strdup and strndup with leaks.  Fix those leaks.
>>
>> gcc/testsuite/ChangeLog:
>>
>>     * gcc.dg/builtin-dynamic-object-size-0.c (test_strdup,
>>     test_strndup, test_strdup_min, test_strndup_min): Free RES
>>     before returning from function.
> We don't generally worry about these kinds of issues in the testsuite. 
> My only worry would be compromising the test.  By adding the free calls 
> the compiler might match up the allocation and release and potentially 
> turn it into an alloca.  I don't think we're likely to do that in this 
> case, but it's worth keeping in mind.

Ack, thanks, I'll keep that in mind.

> So OK as long as you've verified the test still does what it's supposed 
> to do.

I have verified that the test still works correctly and the optimizer 
hasn't done anything funny with the calls, i.e. the str*dup calls and 
free calls are as is.

Thanks,
Sid
diff mbox series

Patch

diff --git a/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c
index 4f1606a486b..f9047a037d9 100644
--- a/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c
+++ b/gcc/testsuite/gcc.dg/builtin-dynamic-object-size-0.c
@@ -486,7 +486,10 @@  __attribute__ ((noinline))
 test_strdup (const char *in)
 {
   char *res = __builtin_strdup (in);
-  return __builtin_dynamic_object_size (res, 0);
+  size_t sz = __builtin_dynamic_object_size (res, 0);
+
+  __builtin_free (res);
+  return sz;
 }
 
 size_t
@@ -494,7 +497,10 @@  __attribute__ ((noinline))
 test_strndup (const char *in, size_t bound)
 {
   char *res = __builtin_strndup (in, bound);
-  return __builtin_dynamic_object_size (res, 0);
+  size_t sz = __builtin_dynamic_object_size (res, 0);
+
+  __builtin_free (res);
+  return sz;
 }
 
 size_t
@@ -502,7 +508,10 @@  __attribute__ ((noinline))
 test_strdup_min (const char *in)
 {
   char *res = __builtin_strdup (in);
-  return __builtin_dynamic_object_size (res, 2);
+  size_t sz = __builtin_dynamic_object_size (res, 2);
+
+  __builtin_free (res);
+  return sz;
 }
 
 size_t
@@ -510,7 +519,10 @@  __attribute__ ((noinline))
 test_strndup_min (const char *in, size_t bound)
 {
   char *res = __builtin_strndup (in, bound);
-  return __builtin_dynamic_object_size (res, 2);
+  size_t sz = __builtin_dynamic_object_size (res, 2);
+
+  __builtin_free (res);
+  return sz;
 }
 
 /* Other tests.  */