diff mbox

Do not call memcpy with a NULL argument (PR gcov-profile/80413).

Message ID 845b3346-f2c9-47ea-827b-7eeacef8d188@suse.cz
State New
Headers show

Commit Message

Martin Liška April 13, 2017, 11:19 a.m. UTC
Hello.

As the function is called with argument equal to zero, the memcpy should be
done just in case alloc is greater than zero.

Patch can bootstrap on ppc64le-redhat-linux and survives regression tests.
And gcov.exp works on x86_64-linux-gnu.

Ready to be installed?
Martin

Comments

Nathan Sidwell April 13, 2017, 11:27 a.m. UTC | #1
On 04/13/2017 07:19 AM, Martin Liška wrote:
> Hello.
>
> As the function is called with argument equal to zero, the memcpy should be
> done just in case alloc is greater than zero.
>
> Patch can bootstrap on ppc64le-redhat-linux and survives regression tests.
> And gcov.exp works on x86_64-linux-gnu.

the patch is ok, but it tripped me up as the diff snipped out how alloc 
was calculated, and I'd forgotten what the buffer[alloc] = 0 bit was for.

If you could amend to add a comment, it's good to go.

something like:

+      buffer[alloc] = 0; /* place nul terminators.  */

nathan
diff mbox

Patch

From 82f609465a9702a0c4ce0c4dbe9c3bd744a3ea34 Mon Sep 17 00:00:00 2001
From: marxin <mliska@suse.cz>
Date: Thu, 13 Apr 2017 10:35:29 +0200
Subject: [PATCH] Do not call memcpy with a NULL argument (PR
 gcov-profile/80413).

gcc/ChangeLog:

2017-04-13  Martin Liska  <mliska@suse.cz>

	PR gcov-profile/80413
	* gcov-io.c (gcov_write_string): Copy to buffer just when
	allocated size is greater than zero.
---
 gcc/gcov-io.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/gcc/gcov-io.c b/gcc/gcov-io.c
index 3b6b022d143..18e0f7f4b1b 100644
--- a/gcc/gcov-io.c
+++ b/gcc/gcov-io.c
@@ -347,8 +347,12 @@  gcov_write_string (const char *string)
   buffer = gcov_write_words (1 + alloc);
 
   buffer[0] = alloc;
-  buffer[alloc] = 0;
-  memcpy (&buffer[1], string, length);
+
+  if (alloc > 0)
+    {
+      buffer[alloc] = 0;
+      memcpy (&buffer[1], string, length);
+    }
 }
 #endif
 
-- 
2.12.2