diff mbox series

Fix Werror=stringop-overflow in target.c

Message ID 7f93d46f-ef76-ffc8-e1fb-ed1da5deb17a@suse.cz
State New
Headers show
Series Fix Werror=stringop-overflow in target.c | expand

Commit Message

Martin Liška Nov. 20, 2017, 1:58 p.m. UTC
Hello.

This is fix of compilation error I see with --enable-offload-targets=nvptx-none=. It's explained
in very detail way here:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83007#c1

Ready for trunk?
Martin

libgomp/ChangeLog:

2017-11-20  Martin Liska  <mliska@suse.cz>

	* target.c (gomp_target_init): Use proper string operation.
---
 libgomp/target.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Comments

Jakub Jelinek Nov. 22, 2017, 8:50 p.m. UTC | #1
On Mon, Nov 20, 2017 at 02:58:49PM +0100, Martin Liška wrote:
> Hello.
> 
> This is fix of compilation error I see with --enable-offload-targets=nvptx-none=. It's explained
> in very detail way here:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83007#c1
> 
> Ready for trunk?
> Martin
> 
> libgomp/ChangeLog:
> 
> 2017-11-20  Martin Liska  <mliska@suse.cz>
> 
> 	* target.c (gomp_target_init): Use proper string operation.
> ---
>  libgomp/target.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)

I've instead committed this version after bootstrapping/regtesting it on
x86_64-linux and i686-linux:

2017-11-22  Jakub Jelinek  <jakub@redhat.com>

	PR libgomp/83106
	* target.c (gomp_target_init): Compute lengths just once and
	use them in both malloc size and subsequent copying.

--- libgomp/target.c.jj	2017-10-28 09:02:06.000000000 +0200
+++ libgomp/target.c	2017-11-22 13:54:56.250444247 +0100
@@ -2656,20 +2656,24 @@ gomp_target_init (void)
     do
       {
 	struct gomp_device_descr current_device;
+	size_t prefix_len, suffix_len, cur_len;
 
 	next = strchr (cur, ',');
 
-	plugin_name = (char *) malloc (1 + (next ? next - cur : strlen (cur))
-				       + strlen (prefix) + strlen (suffix));
+	prefix_len = strlen (prefix);
+	cur_len = next ? next - cur : strlen (cur);
+	suffix_len = strlen (suffix);
+
+	plugin_name = (char *) malloc (prefix_len + cur_len + suffix_len + 1);
 	if (!plugin_name)
 	  {
 	    num_devices = 0;
 	    break;
 	  }
 
-	strcpy (plugin_name, prefix);
-	strncat (plugin_name, cur, next ? next - cur : strlen (cur));
-	strcat (plugin_name, suffix);
+	memcpy (plugin_name, prefix, prefix_len);
+	memcpy (plugin_name + prefix_len, cur, cur_len);
+	memcpy (plugin_name + prefix_len + cur_len, suffix, suffix_len + 1);
 
 	if (gomp_load_plugin_for_device (&current_device, plugin_name))
 	  {


	Jakub
diff mbox series

Patch

diff --git a/libgomp/target.c b/libgomp/target.c
index 8ac05e8c641..4838dc98de6 100644
--- a/libgomp/target.c
+++ b/libgomp/target.c
@@ -2668,7 +2668,10 @@  gomp_target_init (void)
 	  }
 
 	strcpy (plugin_name, prefix);
-	strncat (plugin_name, cur, next ? next - cur : strlen (cur));
+	if (next)
+	  strncat (plugin_name, cur, next - cur);
+	else
+	  strcpy (plugin_name, cur);
 	strcat (plugin_name, suffix);
 
 	if (gomp_load_plugin_for_device (&current_device, plugin_name))