public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 1/3] [D] libiberty: Remove stack buffer in dlang_parse_real
@ 2017-05-26 15:49 Iain Buclaw
  2017-05-26 18:19 ` Ian Lance Taylor via gcc-patches
  0 siblings, 1 reply; 2+ messages in thread
From: Iain Buclaw @ 2017-05-26 15:49 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 280 bytes --]

Hi,

This patch fixes a problem in the D demangler where an abnormally long
mangled floating point literal would cause the program write past the
allocated stack buffer.   This should never happen with a valid D
symbol, but we should be able to handle anything thrown at us.

---

[-- Attachment #2: 01-d-stack-stomp.patch --]
[-- Type: text/x-patch, Size: 2896 bytes --]

commit 62d51a8de1fa6543f11ff0d9f97b3ce714023089
Author: Iain Buclaw <ibuclaw@gdcproject.org>
Date:   Fri May 26 15:29:50 2017 +0200

    libiberty/ChangeLog:
    
    2017-05-26  Iain Buclaw  <ibuclaw@gdcproject.org>
    
            * d-demangle.c (dlang_parse_real): Remove stack buffer, write
            the demangled hexadecimal directly to string.
            * testsuite/d-demangle-expected: Add tests.

diff --git a/libiberty/d-demangle.c b/libiberty/d-demangle.c
index ec5508e2777..030cab3333f 100644
--- a/libiberty/d-demangle.c
+++ b/libiberty/d-demangle.c
@@ -1025,9 +1025,6 @@ dlang_parse_integer (string *decl, const char *mangled, char type)
 static const char *
 dlang_parse_real (string *decl, const char *mangled)
 {
-  char buffer[64];
-  int len = 0;
-
   /* Handle NAN and +-INF.  */
   if (strncmp (mangled, "NAN", 3) == 0)
     {
@@ -1051,23 +1048,22 @@ dlang_parse_real (string *decl, const char *mangled)
   /* Hexadecimal prefix and leading bit.  */
   if (*mangled == 'N')
     {
-      buffer[len++] = '-';
+      string_append (decl, "-");
       mangled++;
     }
 
   if (!ISXDIGIT (*mangled))
     return NULL;
 
-  buffer[len++] = '0';
-  buffer[len++] = 'x';
-  buffer[len++] = *mangled;
-  buffer[len++] = '.';
+  string_append (decl, "0x");
+  string_appendn (decl, mangled, 1);
+  string_append (decl, ".");
   mangled++;
 
   /* Significand.  */
   while (ISXDIGIT (*mangled))
     {
-      buffer[len++] = *mangled;
+      string_appendn (decl, mangled, 1);
       mangled++;
     }
 
@@ -1075,26 +1071,21 @@ dlang_parse_real (string *decl, const char *mangled)
   if (*mangled != 'P')
     return NULL;
 
-  buffer[len++] = 'p';
+  string_append (decl, "p");
   mangled++;
 
   if (*mangled == 'N')
     {
-      buffer[len++] = '-';
+      string_append (decl, "-");
       mangled++;
     }
 
   while (ISDIGIT (*mangled))
     {
-      buffer[len++] = *mangled;
+      string_appendn (decl, mangled, 1);
       mangled++;
     }
 
-  /* Write out the demangled hexadecimal, rather than trying to
-     convert the buffer into a floating-point value.  */
-  buffer[len] = '\0';
-  len = strlen (buffer);
-  string_appendn (decl, buffer, len);
   return mangled;
 }
 
diff --git a/libiberty/testsuite/d-demangle-expected b/libiberty/testsuite/d-demangle-expected
index 626d7c2d980..950d4955d8f 100644
--- a/libiberty/testsuite/d-demangle-expected
+++ b/libiberty/testsuite/d-demangle-expected
@@ -838,6 +838,10 @@ _D8demangle52__T4testVrcN0C4CCCCCCCCCCCCCDP4c0B666666666666666P6Zv
 demangle.test!(-0x0.C4CCCCCCCCCCCCCDp4+0x0.B666666666666666p6i)
 #
 --format=dlang
+_D8demangle91__T4testVde000111222333444555666777888999AAABBBCCCDDDEEEFFFP000111222333444555666777888999Zv
+demangle.test!(0x0.00111222333444555666777888999AAABBBCCCDDDEEEFFFp000111222333444555666777888999)
+#
+--format=dlang
 _D8demangle22__T4testVG3ua3_616263Zv
 demangle.test!("abc")
 #

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

* Re: [PATCH 1/3] [D] libiberty: Remove stack buffer in dlang_parse_real
  2017-05-26 15:49 [PATCH 1/3] [D] libiberty: Remove stack buffer in dlang_parse_real Iain Buclaw
@ 2017-05-26 18:19 ` Ian Lance Taylor via gcc-patches
  0 siblings, 0 replies; 2+ messages in thread
From: Ian Lance Taylor via gcc-patches @ 2017-05-26 18:19 UTC (permalink / raw)
  To: Iain Buclaw; +Cc: gcc-patches

On Fri, May 26, 2017 at 8:48 AM, Iain Buclaw <ibuclaw@gdcproject.org> wrote:
>
> This patch fixes a problem in the D demangler where an abnormally long
> mangled floating point literal would cause the program write past the
> allocated stack buffer.   This should never happen with a valid D
> symbol, but we should be able to handle anything thrown at us.

This is OK.

Thanks.

Ian

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

end of thread, other threads:[~2017-05-26 18:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-26 15:49 [PATCH 1/3] [D] libiberty: Remove stack buffer in dlang_parse_real Iain Buclaw
2017-05-26 18:19 ` Ian Lance Taylor via gcc-patches

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).