public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* asan : use of uninitialized value in buffer_and_nest
@ 2022-02-16 11:31 Alan Modra
  0 siblings, 0 replies; 2+ messages in thread
From: Alan Modra @ 2022-02-16 11:31 UTC (permalink / raw)
  To: binutils

	* macro.c (buffer_and_nest): Don't read past end of string buffer.

diff --git a/gas/macro.c b/gas/macro.c
index 9327a6dea76..cbb9574fd66 100644
--- a/gas/macro.c
+++ b/gas/macro.c
@@ -184,14 +184,24 @@ buffer_and_nest (const char *from, const char *to, sb *ptr,
 	{
 	  if (! flag_m68k_mri && ptr->ptr[i] == '.')
 	    i++;
-	  if (from == NULL
-	     && strncasecmp (ptr->ptr + i, "IRPC", from_len = 4) != 0
-	     && strncasecmp (ptr->ptr + i, "IRP", from_len = 3) != 0
-	     && strncasecmp (ptr->ptr + i, "IREPC", from_len = 5) != 0
-	     && strncasecmp (ptr->ptr + i, "IREP", from_len = 4) != 0
-	     && strncasecmp (ptr->ptr + i, "REPT", from_len = 4) != 0
-	     && strncasecmp (ptr->ptr + i, "REP", from_len = 3) != 0)
-	    from_len = 0;
+	  if (from == NULL)
+	    {
+	      size_t len = ptr->len - i;
+	      if (len >= 5 && strncasecmp (ptr->ptr + i, "IREPC", 5) == 0)
+		from_len = 5;
+	      else if (len >= 4 && strncasecmp (ptr->ptr + i, "IREP", 4) == 0)
+		from_len = 4;
+	      else if (len >= 4 && strncasecmp (ptr->ptr + i, "IRPC", 4) == 0)
+		from_len = 4;
+	      else if (len >= 4 && strncasecmp (ptr->ptr + i, "REPT", 4) == 0)
+		from_len = 4;
+	      else if (len >= 3 && strncasecmp (ptr->ptr + i, "IRP", 3) == 0)
+		from_len = 3;
+	      else if (len >= 3 && strncasecmp (ptr->ptr + i, "REP", 3) == 0)
+		from_len = 3;
+	      else
+		from_len = 0;
+	    }
 	  if ((from != NULL
 	       ? strncasecmp (ptr->ptr + i, from, from_len) == 0
 	       : from_len > 0)
@@ -199,7 +209,8 @@ buffer_and_nest (const char *from, const char *to, sb *ptr,
 		  || ! (is_part_of_name (ptr->ptr[i + from_len])
 			|| is_name_ender (ptr->ptr[i + from_len]))))
 	    depth++;
-	  if (strncasecmp (ptr->ptr + i, to, to_len) == 0
+	  if (ptr->len - i >= to_len
+	      && strncasecmp (ptr->ptr + i, to, to_len) == 0
 	      && (ptr->len == (i + to_len)
 		  || ! (is_part_of_name (ptr->ptr[i + to_len])
 			|| is_name_ender (ptr->ptr[i + to_len]))))

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 2+ messages in thread

* asan: use of uninitialized value in buffer_and_nest
@ 2022-03-17 11:00 Alan Modra
  0 siblings, 0 replies; 2+ messages in thread
From: Alan Modra @ 2022-03-17 11:00 UTC (permalink / raw)
  To: binutils

More occurences of the same as commit d12b8d620c6a.

	* macro.c (buffer_and_nest): Sanity check length in buffer
	before calling strncasecmp.

diff --git a/gas/macro.c b/gas/macro.c
index cbb9574fd66..2228f5b5404 100644
--- a/gas/macro.c
+++ b/gas/macro.c
@@ -184,9 +184,9 @@ buffer_and_nest (const char *from, const char *to, sb *ptr,
 	{
 	  if (! flag_m68k_mri && ptr->ptr[i] == '.')
 	    i++;
+	  size_t len = ptr->len - i;
 	  if (from == NULL)
 	    {
-	      size_t len = ptr->len - i;
 	      if (len >= 5 && strncasecmp (ptr->ptr + i, "IREPC", 5) == 0)
 		from_len = 5;
 	      else if (len >= 4 && strncasecmp (ptr->ptr + i, "IREP", 4) == 0)
@@ -203,15 +203,16 @@ buffer_and_nest (const char *from, const char *to, sb *ptr,
 		from_len = 0;
 	    }
 	  if ((from != NULL
-	       ? strncasecmp (ptr->ptr + i, from, from_len) == 0
+	       ? (len >= from_len
+		  && strncasecmp (ptr->ptr + i, from, from_len) == 0)
 	       : from_len > 0)
-	      && (ptr->len == (i + from_len)
+	      && (len == from_len
 		  || ! (is_part_of_name (ptr->ptr[i + from_len])
 			|| is_name_ender (ptr->ptr[i + from_len]))))
 	    depth++;
-	  if (ptr->len - i >= to_len
+	  if (len >= to_len
 	      && strncasecmp (ptr->ptr + i, to, to_len) == 0
-	      && (ptr->len == (i + to_len)
+	      && (len == to_len
 		  || ! (is_part_of_name (ptr->ptr[i + to_len])
 			|| is_name_ender (ptr->ptr[i + to_len]))))
 	    {
@@ -233,7 +234,7 @@ buffer_and_nest (const char *from, const char *to, sb *ptr,
 	     number when expanding the macro), and since for short
 	     macros we clearly prefer reporting the point of expansion
 	     anyway, there's not an obviously better fix here.  */
-	  if (strncasecmp (ptr->ptr + i, "linefile", 8) == 0)
+	  if (len >= 8 && strncasecmp (ptr->ptr + i, "linefile", 8) == 0)
 	    {
 	      char saved_eol_char = ptr->ptr[ptr->len];
 

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-03-17 11:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-16 11:31 asan : use of uninitialized value in buffer_and_nest Alan Modra
2022-03-17 11:00 asan: " Alan Modra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).