License along with libiberty; see the file COPYING.LIB.
If not, see <http://www.gnu.org/licenses/>. */
-/* This file exports one function; dlang_demangle.
-
- This file imports strtol for decoding mangled literals. */
+/* This file exports one function; dlang_demangle. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
-#else
-extern long strtol (const char *nptr, char **endptr, int base);
#endif
#include <demangle.h>
static const char *dlang_parse_template (string *, const char *, long);
+/* Extract the number from MANGLED, and assign the result to RET.
+ Return the remaining string on success or NULL on failure. */
+static const char *
+dlang_number (const char *mangled, long *ret)
+{
+ /* Return NULL if trying to extract something that isn't a digit. */
+ if (mangled == NULL || !ISDIGIT (*mangled))
+ return NULL;
+
+ (*ret) = 0;
+
+ while (ISDIGIT (*mangled))
+ {
+ (*ret) *= 10;
+
+ /* If an overflow occured when multiplying by ten, the result
+ will not be a multiple of ten. */
+ if ((*ret % 10) != 0)
+ return NULL;
+
+ (*ret) += mangled[0] - '0';
+ mangled++;
+ }
+
+ if (*mangled == '\0' || *ret < 0)
+ return NULL;
+
+ return mangled;
+}
+
/* Demangle the calling convention from MANGLED and append it to DECL.
Return the remaining string on success or NULL on failure. */
static const char *
dlang_identifier (string *decl, const char *mangled,
enum dlang_symbol_kinds kind)
{
- char *endptr;
long len;
+ const char *endptr = dlang_number (mangled, &len);
- if (mangled == NULL || *mangled == '\0')
- return NULL;
-
- len = strtol (mangled, &endptr, 10);
-
- if (endptr == NULL || len <= 0)
+ if (endptr == NULL || len == 0)
return NULL;
/* In template parameter symbols, the first character of the mangled
if (kind == dlang_template_param)
{
long psize = len;
- char *pend;
+ const char *pend;
int saved = string_length (decl);
/* Work backwards until a match is found. */
char value[10];
int pos = 10;
int width = 0;
- char *endptr;
- long val = strtol (mangled, &endptr, 10);
+ long val;
- if (endptr == NULL || val < 0)
+ mangled = dlang_number (mangled, &val);
+ if (mangled == NULL)
return NULL;
string_append (decl, "'");
string_appendn (decl, &(value[pos]), 10 - pos);
}
string_append (decl, "'");
- mangled = endptr;
}
else if (type == 'b')
{
/* Parse boolean value. */
- char *endptr;
- long val = strtol (mangled, &endptr, 10);
+ long val;
- if (endptr == NULL || val < 0)
+ mangled = dlang_number (mangled, &val);
+ if (mangled == NULL)
return NULL;
string_append (decl, val ? "true" : "false");
- mangled = endptr;
}
else
{
const char *numptr = mangled;
size_t num = 0;
+ if (! ISDIGIT (*mangled))
+ return NULL;
+
while (ISDIGIT (*mangled))
{
num++;
dlang_parse_string (string *decl, const char *mangled)
{
char type = *mangled;
- char *endptr;
long len;
mangled++;
- len = strtol (mangled, &endptr, 10);
-
- if (endptr == NULL || len < 0)
- return NULL;
-
- mangled = endptr;
- if (*mangled != '_')
+ mangled = dlang_number (mangled, &len);
+ if (mangled == NULL || *mangled != '_')
return NULL;
mangled++;
static const char *
dlang_parse_arrayliteral (string *decl, const char *mangled)
{
- char *endptr;
- long elements = strtol (mangled, &endptr, 10);
+ long elements;
- if (endptr == NULL || elements < 0)
+ mangled = dlang_number (mangled, &elements);
+ if (mangled == NULL)
return NULL;
- mangled = endptr;
string_append (decl, "[");
while (elements--)
{
static const char *
dlang_parse_assocarray (string *decl, const char *mangled)
{
- char *endptr;
- long elements = strtol (mangled, &endptr, 10);
+ long elements;
- if (endptr == NULL || elements < 0)
+ mangled = dlang_number (mangled, &elements);
+ if (mangled == NULL)
return NULL;
- mangled = endptr;
string_append (decl, "[");
while (elements--)
{
static const char *
dlang_parse_structlit (string *decl, const char *mangled, const char *name)
{
- char *endptr;
- long args = strtol (mangled, &endptr, 10);
+ long args;
- if (endptr == NULL || args < 0)
+ mangled = dlang_number (mangled, &args);
+ if (mangled == NULL)
return NULL;
- mangled = endptr;
if (name != NULL)
string_append (decl, name);
case 'i':
mangled++;
- if (*mangled < '0' || *mangled > '9')
- return NULL;
/* Fall through */
/* There really should always be an `i' before encoded numbers, but there
static const char *
dlang_parse_tuple (string *decl, const char *mangled)
{
- char *endptr;
- long elements = strtol (mangled, &endptr, 10);
+ long elements;
- if (endptr == NULL || elements < 0)
+ mangled = dlang_number (mangled, &elements);
+ if (mangled == NULL)
return NULL;
- mangled = endptr;
string_append (decl, "Tuple!(");
while (elements--)