Coverage Report

Created: 2026-03-10 08:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/libiberty/rust-demangle.c
Line
Count
Source
1
/* Demangler for the Rust programming language
2
   Copyright (C) 2016-2026 Free Software Foundation, Inc.
3
   Written by David Tolnay (dtolnay@gmail.com).
4
   Rewritten by Eduard-Mihai Burtescu (eddyb@lyken.rs) for v0 support.
5
6
This file is part of the libiberty library.
7
Libiberty is free software; you can redistribute it and/or
8
modify it under the terms of the GNU Library General Public
9
License as published by the Free Software Foundation; either
10
version 2 of the License, or (at your option) any later version.
11
12
In addition to the permissions in the GNU Library General Public
13
License, the Free Software Foundation gives you unlimited permission
14
to link the compiled version of this file into combinations with other
15
programs, and to distribute those combinations without any restriction
16
coming from the use of this file.  (The Library Public License
17
restrictions do apply in other respects; for example, they cover
18
modification of the file, and distribution when not linked into a
19
combined executable.)
20
21
Libiberty is distributed in the hope that it will be useful,
22
but WITHOUT ANY WARRANTY; without even the implied warranty of
23
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24
Library General Public License for more details.
25
26
You should have received a copy of the GNU Library General Public
27
License along with libiberty; see the file COPYING.LIB.
28
If not, see <http://www.gnu.org/licenses/>.  */
29
30
31
#ifdef HAVE_CONFIG_H
32
#include "config.h"
33
#endif
34
35
#include "safe-ctype.h"
36
37
#include <inttypes.h>
38
#include <sys/types.h>
39
#include <string.h>
40
#include <stdio.h>
41
#include <stdlib.h>
42
43
#ifdef HAVE_STRING_H
44
#include <string.h>
45
#else
46
extern size_t strlen(const char *s);
47
extern int strncmp(const char *s1, const char *s2, size_t n);
48
extern void *memset(void *s, int c, size_t n);
49
#endif
50
51
#include <demangle.h>
52
#include "libiberty.h"
53
54
struct rust_demangler
55
{
56
  const char *sym;
57
  size_t sym_len;
58
59
  void *callback_opaque;
60
  demangle_callbackref callback;
61
62
  /* Position of the next character to read from the symbol. */
63
  size_t next;
64
65
  /* Non-zero if any error occurred. */
66
  int errored;
67
68
  /* Non-zero if nothing should be printed. */
69
  int skipping_printing;
70
71
  /* Non-zero if printing should be verbose (e.g. include hashes). */
72
  int verbose;
73
74
  /* Rust mangling version, with legacy mangling being -1. */
75
  int version;
76
77
  /* Recursion depth.  */
78
  unsigned int recursion;
79
  /* Maximum number of times demangle_path may be called recursively.  */
80
0
#define RUST_MAX_RECURSION_COUNT  1024
81
0
#define RUST_NO_RECURSION_LIMIT   ((unsigned int) -1)
82
83
  uint64_t bound_lifetime_depth;
84
};
85
86
/* Parsing functions. */
87
88
static char
89
peek (const struct rust_demangler *rdm)
90
0
{
91
0
  if (rdm->next < rdm->sym_len)
92
0
    return rdm->sym[rdm->next];
93
0
  return 0;
94
0
}
95
96
static int
97
eat (struct rust_demangler *rdm, char c)
98
0
{
99
0
  if (peek (rdm) == c)
100
0
    {
101
0
      rdm->next++;
102
0
      return 1;
103
0
    }
104
0
  else
105
0
    return 0;
106
0
}
107
108
static char
109
next (struct rust_demangler *rdm)
110
0
{
111
0
  char c = peek (rdm);
112
0
  if (!c)
113
0
    rdm->errored = 1;
114
0
  else
115
0
    rdm->next++;
116
0
  return c;
117
0
}
118
119
static uint64_t
120
parse_integer_62 (struct rust_demangler *rdm)
121
0
{
122
0
  char c;
123
0
  uint64_t x;
124
125
0
  if (eat (rdm, '_'))
126
0
    return 0;
127
128
0
  x = 0;
129
0
  while (!eat (rdm, '_') && !rdm->errored)
130
0
    {
131
0
      c = next (rdm);
132
0
      x *= 62;
133
0
      if (ISDIGIT (c))
134
0
        x += c - '0';
135
0
      else if (ISLOWER (c))
136
0
        x += 10 + (c - 'a');
137
0
      else if (ISUPPER (c))
138
0
        x += 10 + 26 + (c - 'A');
139
0
      else
140
0
        {
141
0
          rdm->errored = 1;
142
0
          return 0;
143
0
        }
144
0
    }
145
0
  return x + 1;
146
0
}
147
148
static uint64_t
149
parse_opt_integer_62 (struct rust_demangler *rdm, char tag)
150
0
{
151
0
  if (!eat (rdm, tag))
152
0
    return 0;
153
0
  return 1 + parse_integer_62 (rdm);
154
0
}
155
156
static uint64_t
157
parse_disambiguator (struct rust_demangler *rdm)
158
0
{
159
0
  return parse_opt_integer_62 (rdm, 's');
160
0
}
161
162
static size_t
163
parse_hex_nibbles (struct rust_demangler *rdm, uint64_t *value)
164
0
{
165
0
  char c;
166
0
  size_t hex_len;
167
168
0
  hex_len = 0;
169
0
  *value = 0;
170
171
0
  while (!eat (rdm, '_'))
172
0
    {
173
0
      *value <<= 4;
174
175
0
      c = next (rdm);
176
0
      if (ISDIGIT (c))
177
0
        *value |= c - '0';
178
0
      else if (c >= 'a' && c <= 'f')
179
0
        *value |= 10 + (c - 'a');
180
0
      else
181
0
        {
182
0
          rdm->errored = 1;
183
0
          return 0;
184
0
        }
185
0
      hex_len++;
186
0
    }
187
188
0
  return hex_len;
189
0
}
190
191
struct rust_mangled_ident
192
{
193
  /* ASCII part of the identifier. */
194
  const char *ascii;
195
  size_t ascii_len;
196
197
  /* Punycode insertion codes for Unicode codepoints, if any. */
198
  const char *punycode;
199
  size_t punycode_len;
200
};
201
202
static struct rust_mangled_ident
203
parse_ident (struct rust_demangler *rdm)
204
0
{
205
0
  char c;
206
0
  size_t start, len;
207
0
  int is_punycode = 0;
208
0
  struct rust_mangled_ident ident;
209
210
0
  ident.ascii = NULL;
211
0
  ident.ascii_len = 0;
212
0
  ident.punycode = NULL;
213
0
  ident.punycode_len = 0;
214
215
0
  if (rdm->version != -1)
216
0
    is_punycode = eat (rdm, 'u');
217
218
0
  c = next (rdm);
219
0
  if (!ISDIGIT (c))
220
0
    {
221
0
      rdm->errored = 1;
222
0
      return ident;
223
0
    }
224
0
  len = c - '0';
225
226
0
  if (c != '0')
227
0
    while (ISDIGIT (peek (rdm)))
228
0
      len = len * 10 + (next (rdm) - '0');
229
230
  /* Skip past the optional `_` separator (v0). */
231
0
  if (rdm->version != -1)
232
0
    eat (rdm, '_');
233
234
0
  start = rdm->next;
235
0
  rdm->next += len;
236
  /* Check for overflows. */
237
0
  if ((start > rdm->next) || (rdm->next > rdm->sym_len))
238
0
    {
239
0
      rdm->errored = 1;
240
0
      return ident;
241
0
    }
242
243
0
  ident.ascii = rdm->sym + start;
244
0
  ident.ascii_len = len;
245
246
0
  if (is_punycode)
247
0
    {
248
0
      ident.punycode_len = 0;
249
0
      while (ident.ascii_len > 0)
250
0
        {
251
0
          ident.ascii_len--;
252
253
          /* The last '_' is a separator between ascii & punycode. */
254
0
          if (ident.ascii[ident.ascii_len] == '_')
255
0
            break;
256
257
0
          ident.punycode_len++;
258
0
        }
259
0
      if (!ident.punycode_len)
260
0
        {
261
0
          rdm->errored = 1;
262
0
          return ident;
263
0
        }
264
0
      ident.punycode = ident.ascii + (len - ident.punycode_len);
265
0
    }
266
267
0
  if (ident.ascii_len == 0)
268
0
    ident.ascii = NULL;
269
270
0
  return ident;
271
0
}
272
273
/* Printing functions. */
274
275
static void
276
print_str (struct rust_demangler *rdm, const char *data, size_t len)
277
0
{
278
0
  if (!rdm->errored && !rdm->skipping_printing)
279
0
    rdm->callback (data, len, rdm->callback_opaque);
280
0
}
281
282
0
#define PRINT(s) print_str (rdm, s, strlen (s))
283
284
static void
285
print_uint64 (struct rust_demangler *rdm, uint64_t x)
286
0
{
287
0
  char s[21];
288
0
  snprintf (s, 21, "%" PRIu64, x);
289
0
  PRINT (s);
290
0
}
291
292
static void
293
print_uint64_hex (struct rust_demangler *rdm, uint64_t x)
294
0
{
295
0
  char s[17];
296
0
  snprintf (s, 17, "%" PRIx64, x);
297
0
  PRINT (s);
298
0
}
299
300
/* Return a 0x0-0xf value if the char is 0-9a-f, and -1 otherwise. */
301
static int
302
decode_lower_hex_nibble (char nibble)
303
0
{
304
0
  if ('0' <= nibble && nibble <= '9')
305
0
    return nibble - '0';
306
0
  if ('a' <= nibble && nibble <= 'f')
307
0
    return 0xa + (nibble - 'a');
308
0
  return -1;
309
0
}
310
311
/* Return the unescaped character for a "$...$" escape, or 0 if invalid. */
312
static char
313
decode_legacy_escape (const char *e, size_t len, size_t *out_len)
314
0
{
315
0
  char c = 0;
316
0
  size_t escape_len = 0;
317
0
  int lo_nibble = -1, hi_nibble = -1;
318
319
0
  if (len < 3 || e[0] != '$')
320
0
    return 0;
321
322
0
  e++;
323
0
  len--;
324
325
0
  if (e[0] == 'C')
326
0
    {
327
0
      escape_len = 1;
328
329
0
      c = ',';
330
0
    }
331
0
  else if (len > 2)
332
0
    {
333
0
      escape_len = 2;
334
335
0
      if (e[0] == 'S' && e[1] == 'P')
336
0
        c = '@';
337
0
      else if (e[0] == 'B' && e[1] == 'P')
338
0
        c = '*';
339
0
      else if (e[0] == 'R' && e[1] == 'F')
340
0
        c = '&';
341
0
      else if (e[0] == 'L' && e[1] == 'T')
342
0
        c = '<';
343
0
      else if (e[0] == 'G' && e[1] == 'T')
344
0
        c = '>';
345
0
      else if (e[0] == 'L' && e[1] == 'P')
346
0
        c = '(';
347
0
      else if (e[0] == 'R' && e[1] == 'P')
348
0
        c = ')';
349
0
      else if (e[0] == 'u' && len > 3)
350
0
        {
351
0
          escape_len = 3;
352
353
0
          hi_nibble = decode_lower_hex_nibble (e[1]);
354
0
          if (hi_nibble < 0)
355
0
            return 0;
356
0
          lo_nibble = decode_lower_hex_nibble (e[2]);
357
0
          if (lo_nibble < 0)
358
0
            return 0;
359
360
          /* Only allow non-control ASCII characters. */
361
0
          if (hi_nibble > 7)
362
0
            return 0;
363
0
          c = (hi_nibble << 4) | lo_nibble;
364
0
          if (c < 0x20)
365
0
            return 0;
366
0
        }
367
0
    }
368
369
0
  if (!c || len <= escape_len || e[escape_len] != '$')
370
0
    return 0;
371
372
0
  *out_len = 2 + escape_len;
373
0
  return c;
374
0
}
375
376
static void
377
print_ident (struct rust_demangler *rdm, struct rust_mangled_ident ident)
378
0
{
379
0
  char unescaped;
380
0
  uint8_t *out, *p, d;
381
0
  size_t len, cap, punycode_pos, j;
382
  /* Punycode parameters and state. */
383
0
  uint32_t c;
384
0
  size_t base, t_min, t_max, skew, damp, bias, i;
385
0
  size_t delta, w, k, t;
386
387
0
  if (rdm->errored || rdm->skipping_printing)
388
0
    return;
389
390
0
  if (rdm->version == -1)
391
0
    {
392
      /* Ignore leading underscores preceding escape sequences.
393
         The mangler inserts an underscore to make sure the
394
         identifier begins with a XID_Start character. */
395
0
      if (ident.ascii_len >= 2 && ident.ascii[0] == '_'
396
0
          && ident.ascii[1] == '$')
397
0
        {
398
0
          ident.ascii++;
399
0
          ident.ascii_len--;
400
0
        }
401
402
0
      while (ident.ascii_len > 0)
403
0
        {
404
          /* Handle legacy escape sequences ("$...$", ".." or "."). */
405
0
          if (ident.ascii[0] == '$')
406
0
            {
407
0
              unescaped
408
0
                  = decode_legacy_escape (ident.ascii, ident.ascii_len, &len);
409
0
              if (unescaped)
410
0
                print_str (rdm, &unescaped, 1);
411
0
              else
412
0
                {
413
                  /* Unexpected escape sequence, print the rest verbatim. */
414
0
                  print_str (rdm, ident.ascii, ident.ascii_len);
415
0
                  return;
416
0
                }
417
0
            }
418
0
          else if (ident.ascii[0] == '.')
419
0
            {
420
0
              if (ident.ascii_len >= 2 && ident.ascii[1] == '.')
421
0
                {
422
                  /* ".." becomes "::" */
423
0
                  PRINT ("::");
424
0
                  len = 2;
425
0
                }
426
0
              else
427
0
                {
428
0
                  PRINT (".");
429
0
                  len = 1;
430
0
                }
431
0
            }
432
0
          else
433
0
            {
434
              /* Print everything before the next escape sequence, at once. */
435
0
              for (len = 0; len < ident.ascii_len; len++)
436
0
                if (ident.ascii[len] == '$' || ident.ascii[len] == '.')
437
0
                  break;
438
439
0
              print_str (rdm, ident.ascii, len);
440
0
            }
441
442
0
          ident.ascii += len;
443
0
          ident.ascii_len -= len;
444
0
        }
445
446
0
      return;
447
0
    }
448
449
0
  if (!ident.punycode)
450
0
    {
451
0
      print_str (rdm, ident.ascii, ident.ascii_len);
452
0
      return;
453
0
    }
454
455
0
  len = 0;
456
0
  cap = 4;
457
0
  while (cap < ident.ascii_len)
458
0
    {
459
0
      cap *= 2;
460
      /* Check for overflows. */
461
0
      if ((cap * 4) / 4 != cap)
462
0
        {
463
0
          rdm->errored = 1;
464
0
          return;
465
0
        }
466
0
    }
467
468
  /* Store the output codepoints as groups of 4 UTF-8 bytes. */
469
0
  out = (uint8_t *)malloc (cap * 4);
470
0
  if (!out)
471
0
    {
472
0
      rdm->errored = 1;
473
0
      return;
474
0
    }
475
476
  /* Populate initial output from ASCII fragment. */
477
0
  for (len = 0; len < ident.ascii_len; len++)
478
0
    {
479
0
      p = out + 4 * len;
480
0
      p[0] = 0;
481
0
      p[1] = 0;
482
0
      p[2] = 0;
483
0
      p[3] = ident.ascii[len];
484
0
    }
485
486
  /* Punycode parameters and initial state. */
487
0
  base = 36;
488
0
  t_min = 1;
489
0
  t_max = 26;
490
0
  skew = 38;
491
0
  damp = 700;
492
0
  bias = 72;
493
0
  i = 0;
494
0
  c = 0x80;
495
496
0
  punycode_pos = 0;
497
0
  while (punycode_pos < ident.punycode_len)
498
0
    {
499
      /* Read one delta value. */
500
0
      delta = 0;
501
0
      w = 1;
502
0
      k = 0;
503
0
      do
504
0
        {
505
0
          k += base;
506
0
          t = k < bias ? 0 : (k - bias);
507
0
          if (t < t_min)
508
0
            t = t_min;
509
0
          if (t > t_max)
510
0
            t = t_max;
511
512
0
          if (punycode_pos >= ident.punycode_len)
513
0
            goto cleanup;
514
0
          d = ident.punycode[punycode_pos++];
515
516
0
          if (ISLOWER (d))
517
0
            d = d - 'a';
518
0
          else if (ISDIGIT (d))
519
0
            d = 26 + (d - '0');
520
0
          else
521
0
            {
522
0
              rdm->errored = 1;
523
0
              goto cleanup;
524
0
            }
525
526
0
          delta += d * w;
527
0
          w *= base - t;
528
0
        }
529
0
      while (d >= t);
530
531
      /* Compute the new insert position and character. */
532
0
      len++;
533
0
      i += delta;
534
0
      c += i / len;
535
0
      i %= len;
536
537
      /* Ensure enough space is available. */
538
0
      if (cap < len)
539
0
        {
540
0
          cap *= 2;
541
          /* Check for overflows. */
542
0
          if ((cap * 4) / 4 != cap || cap < len)
543
0
            {
544
0
              rdm->errored = 1;
545
0
              goto cleanup;
546
0
            }
547
0
        }
548
0
      p = (uint8_t *)realloc (out, cap * 4);
549
0
      if (!p)
550
0
        {
551
0
          rdm->errored = 1;
552
0
          goto cleanup;
553
0
        }
554
0
      out = p;
555
556
      /* Move the characters after the insert position. */
557
0
      p = out + i * 4;
558
0
      memmove (p + 4, p, (len - i - 1) * 4);
559
560
      /* Insert the new character, as UTF-8 bytes. */
561
0
      p[0] = c >= 0x10000 ? 0xf0 | (c >> 18) : 0;
562
0
      p[1] = c >= 0x800 ? (c < 0x10000 ? 0xe0 : 0x80) | ((c >> 12) & 0x3f) : 0;
563
0
      p[2] = (c < 0x800 ? 0xc0 : 0x80) | ((c >> 6) & 0x3f);
564
0
      p[3] = 0x80 | (c & 0x3f);
565
566
      /* If there are no more deltas, decoding is complete. */
567
0
      if (punycode_pos == ident.punycode_len)
568
0
        break;
569
570
0
      i++;
571
572
      /* Perform bias adaptation. */
573
0
      delta /= damp;
574
0
      damp = 2;
575
576
0
      delta += delta / len;
577
0
      k = 0;
578
0
      while (delta > ((base - t_min) * t_max) / 2)
579
0
        {
580
0
          delta /= base - t_min;
581
0
          k += base;
582
0
        }
583
0
      bias = k + ((base - t_min + 1) * delta) / (delta + skew);
584
0
    }
585
586
  /* Remove all the 0 bytes to leave behind an UTF-8 string. */
587
0
  for (i = 0, j = 0; i < len * 4; i++)
588
0
    if (out[i] != 0)
589
0
      out[j++] = out[i];
590
591
0
  print_str (rdm, (const char *)out, j);
592
593
0
cleanup:
594
0
  free (out);
595
0
}
596
597
/* Print the lifetime according to the previously decoded index.
598
   An index of `0` always refers to `'_`, but starting with `1`,
599
   indices refer to late-bound lifetimes introduced by a binder. */
600
static void
601
print_lifetime_from_index (struct rust_demangler *rdm, uint64_t lt)
602
0
{
603
0
  char c;
604
0
  uint64_t depth;
605
606
0
  PRINT ("'");
607
0
  if (lt == 0)
608
0
    {
609
0
      PRINT ("_");
610
0
      return;
611
0
    }
612
613
0
  depth = rdm->bound_lifetime_depth - lt;
614
  /* Try to print lifetimes alphabetically first. */
615
0
  if (depth < 26)
616
0
    {
617
0
      c = 'a' + depth;
618
0
      print_str (rdm, &c, 1);
619
0
    }
620
0
  else
621
0
    {
622
      /* Use `'_123` after running out of letters. */
623
0
      PRINT ("_");
624
0
      print_uint64 (rdm, depth);
625
0
    }
626
0
}
627
628
/* Demangling functions. */
629
630
static void demangle_binder (struct rust_demangler *rdm);
631
static void demangle_path (struct rust_demangler *rdm, int in_value);
632
static void demangle_generic_arg (struct rust_demangler *rdm);
633
static void demangle_type (struct rust_demangler *rdm);
634
static int demangle_path_maybe_open_generics (struct rust_demangler *rdm);
635
static void demangle_dyn_trait (struct rust_demangler *rdm);
636
static void demangle_const (struct rust_demangler *rdm);
637
static void demangle_const_uint (struct rust_demangler *rdm);
638
static void demangle_const_int (struct rust_demangler *rdm);
639
static void demangle_const_bool (struct rust_demangler *rdm);
640
static void demangle_const_char (struct rust_demangler *rdm);
641
642
/* Optionally enter a binder ('G') for late-bound lifetimes,
643
   printing e.g. `for<'a, 'b> `, and make those lifetimes visible
644
   to the caller (via depth level, which the caller should reset). */
645
static void
646
demangle_binder (struct rust_demangler *rdm)
647
0
{
648
0
  uint64_t i, bound_lifetimes;
649
650
0
  if (rdm->errored)
651
0
    return;
652
653
0
  bound_lifetimes = parse_opt_integer_62 (rdm, 'G');
654
  /* Reject implausibly large lifetime counts to prevent
655
     resource exhaustion from crafted symbols (PR demangler/106641).  */
656
0
  if (bound_lifetimes > 1024)
657
0
    {
658
0
      rdm->errored = 1;
659
0
      return;
660
0
    }
661
0
  if (bound_lifetimes > 0)
662
0
    {
663
0
      PRINT ("for<");
664
0
      for (i = 0; i < bound_lifetimes && !rdm->errored; i++)
665
0
        {
666
0
          if (i > 0)
667
0
            PRINT (", ");
668
0
          rdm->bound_lifetime_depth++;
669
0
          print_lifetime_from_index (rdm, 1);
670
0
        }
671
0
      PRINT ("> ");
672
0
    }
673
0
}
674
675
static void
676
demangle_path (struct rust_demangler *rdm, int in_value)
677
0
{
678
0
  char tag, ns;
679
0
  int was_skipping_printing;
680
0
  size_t i, backref, old_next;
681
0
  uint64_t dis;
682
0
  struct rust_mangled_ident name;
683
684
0
  if (rdm->errored)
685
0
    return;
686
687
0
  if (rdm->recursion != RUST_NO_RECURSION_LIMIT)
688
0
    {
689
0
      ++ rdm->recursion;
690
0
      if (rdm->recursion > RUST_MAX_RECURSION_COUNT)
691
  /* FIXME: There ought to be a way to report
692
     that the recursion limit has been reached.  */
693
0
  goto fail_return;
694
0
    }
695
696
0
  switch (tag = next (rdm))
697
0
    {
698
0
    case 'C':
699
0
      dis = parse_disambiguator (rdm);
700
0
      name = parse_ident (rdm);
701
702
0
      print_ident (rdm, name);
703
0
      if (rdm->verbose)
704
0
        {
705
0
          PRINT ("[");
706
0
          print_uint64_hex (rdm, dis);
707
0
          PRINT ("]");
708
0
        }
709
0
      break;
710
0
    case 'N':
711
0
      ns = next (rdm);
712
0
      if (!ISLOWER (ns) && !ISUPPER (ns))
713
0
  goto fail_return;
714
715
0
      demangle_path (rdm, in_value);
716
717
0
      dis = parse_disambiguator (rdm);
718
0
      name = parse_ident (rdm);
719
720
0
      if (ISUPPER (ns))
721
0
        {
722
          /* Special namespaces, like closures and shims. */
723
0
          PRINT ("::{");
724
0
          switch (ns)
725
0
            {
726
0
            case 'C':
727
0
              PRINT ("closure");
728
0
              break;
729
0
            case 'S':
730
0
              PRINT ("shim");
731
0
              break;
732
0
            default:
733
0
              print_str (rdm, &ns, 1);
734
0
            }
735
0
          if (name.ascii || name.punycode)
736
0
            {
737
0
              PRINT (":");
738
0
              print_ident (rdm, name);
739
0
            }
740
0
          PRINT ("#");
741
0
          print_uint64 (rdm, dis);
742
0
          PRINT ("}");
743
0
        }
744
0
      else
745
0
        {
746
          /* Implementation-specific/unspecified namespaces. */
747
748
0
          if (name.ascii || name.punycode)
749
0
            {
750
0
              PRINT ("::");
751
0
              print_ident (rdm, name);
752
0
            }
753
0
        }
754
0
      break;
755
0
    case 'M':
756
0
    case 'X':
757
      /* Ignore the `impl`'s own path.*/
758
0
      parse_disambiguator (rdm);
759
0
      was_skipping_printing = rdm->skipping_printing;
760
0
      rdm->skipping_printing = 1;
761
0
      demangle_path (rdm, in_value);
762
0
      rdm->skipping_printing = was_skipping_printing;
763
      /* fallthrough */
764
0
    case 'Y':
765
0
      PRINT ("<");
766
0
      demangle_type (rdm);
767
0
      if (tag != 'M')
768
0
        {
769
0
          PRINT (" as ");
770
0
          demangle_path (rdm, 0);
771
0
        }
772
0
      PRINT (">");
773
0
      break;
774
0
    case 'I':
775
0
      demangle_path (rdm, in_value);
776
0
      if (in_value)
777
0
        PRINT ("::");
778
0
      PRINT ("<");
779
0
      for (i = 0; !rdm->errored && !eat (rdm, 'E'); i++)
780
0
        {
781
0
          if (i > 0)
782
0
            PRINT (", ");
783
0
          demangle_generic_arg (rdm);
784
0
        }
785
0
      PRINT (">");
786
0
      break;
787
0
    case 'B':
788
0
      backref = parse_integer_62 (rdm);
789
0
      if (!rdm->skipping_printing)
790
0
        {
791
0
          old_next = rdm->next;
792
0
          rdm->next = backref;
793
0
          demangle_path (rdm, in_value);
794
0
          rdm->next = old_next;
795
0
        }
796
0
      break;
797
0
    default:
798
0
      goto fail_return;
799
0
    }
800
0
  goto pass_return;
801
802
0
 fail_return:
803
0
  rdm->errored = 1;
804
0
 pass_return:
805
0
  if (rdm->recursion != RUST_NO_RECURSION_LIMIT)
806
0
    -- rdm->recursion;
807
0
}
808
809
static void
810
demangle_generic_arg (struct rust_demangler *rdm)
811
0
{
812
0
  uint64_t lt;
813
0
  if (eat (rdm, 'L'))
814
0
    {
815
0
      lt = parse_integer_62 (rdm);
816
0
      print_lifetime_from_index (rdm, lt);
817
0
    }
818
0
  else if (eat (rdm, 'K'))
819
0
    demangle_const (rdm);
820
0
  else
821
0
    demangle_type (rdm);
822
0
}
823
824
static const char *
825
basic_type (char tag)
826
0
{
827
0
  switch (tag)
828
0
    {
829
0
    case 'b':
830
0
      return "bool";
831
0
    case 'c':
832
0
      return "char";
833
0
    case 'e':
834
0
      return "str";
835
0
    case 'u':
836
0
      return "()";
837
0
    case 'a':
838
0
      return "i8";
839
0
    case 's':
840
0
      return "i16";
841
0
    case 'l':
842
0
      return "i32";
843
0
    case 'x':
844
0
      return "i64";
845
0
    case 'n':
846
0
      return "i128";
847
0
    case 'i':
848
0
      return "isize";
849
0
    case 'h':
850
0
      return "u8";
851
0
    case 't':
852
0
      return "u16";
853
0
    case 'm':
854
0
      return "u32";
855
0
    case 'y':
856
0
      return "u64";
857
0
    case 'o':
858
0
      return "u128";
859
0
    case 'j':
860
0
      return "usize";
861
0
    case 'f':
862
0
      return "f32";
863
0
    case 'd':
864
0
      return "f64";
865
0
    case 'z':
866
0
      return "!";
867
0
    case 'p':
868
0
      return "_";
869
0
    case 'v':
870
0
      return "...";
871
872
0
    default:
873
0
      return NULL;
874
0
    }
875
0
}
876
877
static void
878
demangle_type (struct rust_demangler *rdm)
879
0
{
880
0
  char tag;
881
0
  size_t i, old_next, backref;
882
0
  uint64_t lt, old_bound_lifetime_depth;
883
0
  const char *basic;
884
0
  struct rust_mangled_ident abi;
885
886
0
  if (rdm->errored)
887
0
    return;
888
889
0
  tag = next (rdm);
890
891
0
  basic = basic_type (tag);
892
0
  if (basic)
893
0
    {
894
0
      PRINT (basic);
895
0
      return;
896
0
    }
897
898
0
   if (rdm->recursion != RUST_NO_RECURSION_LIMIT)
899
0
    {
900
0
      ++ rdm->recursion;
901
0
      if (rdm->recursion > RUST_MAX_RECURSION_COUNT)
902
  /* FIXME: There ought to be a way to report
903
     that the recursion limit has been reached.  */
904
0
  {
905
0
    rdm->errored = 1;
906
0
    -- rdm->recursion;
907
0
    return;
908
0
  }
909
0
    }
910
911
0
  switch (tag)
912
0
    {
913
0
    case 'R':
914
0
    case 'Q':
915
0
      PRINT ("&");
916
0
      if (eat (rdm, 'L'))
917
0
        {
918
0
          lt = parse_integer_62 (rdm);
919
0
          if (lt)
920
0
            {
921
0
              print_lifetime_from_index (rdm, lt);
922
0
              PRINT (" ");
923
0
            }
924
0
        }
925
0
      if (tag != 'R')
926
0
        PRINT ("mut ");
927
0
      demangle_type (rdm);
928
0
      break;
929
0
    case 'P':
930
0
    case 'O':
931
0
      PRINT ("*");
932
0
      if (tag != 'P')
933
0
        PRINT ("mut ");
934
0
      else
935
0
        PRINT ("const ");
936
0
      demangle_type (rdm);
937
0
      break;
938
0
    case 'A':
939
0
    case 'S':
940
0
      PRINT ("[");
941
0
      demangle_type (rdm);
942
0
      if (tag == 'A')
943
0
        {
944
0
          PRINT ("; ");
945
0
          demangle_const (rdm);
946
0
        }
947
0
      PRINT ("]");
948
0
      break;
949
0
    case 'T':
950
0
      PRINT ("(");
951
0
      for (i = 0; !rdm->errored && !eat (rdm, 'E'); i++)
952
0
        {
953
0
          if (i > 0)
954
0
            PRINT (", ");
955
0
          demangle_type (rdm);
956
0
        }
957
0
      if (i == 1)
958
0
        PRINT (",");
959
0
      PRINT (")");
960
0
      break;
961
0
    case 'F':
962
0
      old_bound_lifetime_depth = rdm->bound_lifetime_depth;
963
0
      demangle_binder (rdm);
964
965
0
      if (eat (rdm, 'U'))
966
0
        PRINT ("unsafe ");
967
968
0
      if (eat (rdm, 'K'))
969
0
        {
970
0
          if (eat (rdm, 'C'))
971
0
            {
972
0
              abi.ascii = "C";
973
0
              abi.ascii_len = 1;
974
0
            }
975
0
          else
976
0
            {
977
0
              abi = parse_ident (rdm);
978
0
              if (!abi.ascii || abi.punycode)
979
0
                {
980
0
                  rdm->errored = 1;
981
0
                  goto restore;
982
0
                }
983
0
            }
984
985
0
          PRINT ("extern \"");
986
987
          /* If the ABI had any `-`, they were replaced with `_`,
988
             so the parts between `_` have to be re-joined with `-`. */
989
0
          for (i = 0; i < abi.ascii_len; i++)
990
0
            {
991
0
              if (abi.ascii[i] == '_')
992
0
                {
993
0
                  print_str (rdm, abi.ascii, i);
994
0
                  PRINT ("-");
995
0
                  abi.ascii += i + 1;
996
0
                  abi.ascii_len -= i + 1;
997
0
                  i = 0;
998
0
                }
999
0
            }
1000
0
          print_str (rdm, abi.ascii, abi.ascii_len);
1001
1002
0
          PRINT ("\" ");
1003
0
        }
1004
1005
0
      PRINT ("fn(");
1006
0
      for (i = 0; !rdm->errored && !eat (rdm, 'E'); i++)
1007
0
        {
1008
0
          if (i > 0)
1009
0
            PRINT (", ");
1010
0
          demangle_type (rdm);
1011
0
        }
1012
0
      PRINT (")");
1013
1014
0
      if (eat (rdm, 'u'))
1015
0
        {
1016
          /* Skip printing the return type if it's 'u', i.e. `()`. */
1017
0
        }
1018
0
      else
1019
0
        {
1020
0
          PRINT (" -> ");
1021
0
          demangle_type (rdm);
1022
0
        }
1023
1024
    /* Restore `bound_lifetime_depth` to outside the binder. */
1025
0
    restore:
1026
0
      rdm->bound_lifetime_depth = old_bound_lifetime_depth;
1027
0
      break;
1028
0
    case 'D':
1029
0
      PRINT ("dyn ");
1030
1031
0
      old_bound_lifetime_depth = rdm->bound_lifetime_depth;
1032
0
      demangle_binder (rdm);
1033
1034
0
      for (i = 0; !rdm->errored && !eat (rdm, 'E'); i++)
1035
0
        {
1036
0
          if (i > 0)
1037
0
            PRINT (" + ");
1038
0
          demangle_dyn_trait (rdm);
1039
0
        }
1040
1041
      /* Restore `bound_lifetime_depth` to outside the binder. */
1042
0
      rdm->bound_lifetime_depth = old_bound_lifetime_depth;
1043
1044
0
      if (!eat (rdm, 'L'))
1045
0
        {
1046
0
          rdm->errored = 1;
1047
0
          return;
1048
0
        }
1049
0
      lt = parse_integer_62 (rdm);
1050
0
      if (lt)
1051
0
        {
1052
0
          PRINT (" + ");
1053
0
          print_lifetime_from_index (rdm, lt);
1054
0
        }
1055
0
      break;
1056
0
    case 'B':
1057
0
      backref = parse_integer_62 (rdm);
1058
0
      if (!rdm->skipping_printing)
1059
0
        {
1060
0
          old_next = rdm->next;
1061
0
          rdm->next = backref;
1062
0
          demangle_type (rdm);
1063
0
          rdm->next = old_next;
1064
0
        }
1065
0
      break;
1066
0
    default:
1067
      /* Go back to the tag, so `demangle_path` also sees it. */
1068
0
      rdm->next--;
1069
0
      demangle_path (rdm, 0);
1070
0
    }
1071
1072
0
  if (rdm->recursion != RUST_NO_RECURSION_LIMIT)
1073
0
    -- rdm->recursion;
1074
0
}
1075
1076
/* A trait in a trait object may have some "existential projections"
1077
   (i.e. associated type bindings) after it, which should be printed
1078
   in the `<...>` of the trait, e.g. `dyn Trait<T, U, Assoc=X>`.
1079
   To this end, this method will keep the `<...>` of an 'I' path
1080
   open, by omitting the `>`, and return `Ok(true)` in that case. */
1081
static int
1082
demangle_path_maybe_open_generics (struct rust_demangler *rdm)
1083
0
{
1084
0
  int open;
1085
0
  size_t i, old_next, backref;
1086
1087
0
  open = 0;
1088
1089
0
  if (rdm->errored)
1090
0
    return open;
1091
1092
0
  if (rdm->recursion != RUST_NO_RECURSION_LIMIT)
1093
0
    {
1094
0
      ++ rdm->recursion;
1095
0
      if (rdm->recursion > RUST_MAX_RECURSION_COUNT)
1096
0
  {
1097
    /* FIXME: There ought to be a way to report
1098
       that the recursion limit has been reached.  */
1099
0
    rdm->errored = 1;
1100
0
    goto end_of_func;
1101
0
  }
1102
0
    }
1103
1104
0
  if (eat (rdm, 'B'))
1105
0
    {
1106
0
      backref = parse_integer_62 (rdm);
1107
0
      if (!rdm->skipping_printing)
1108
0
        {
1109
0
          old_next = rdm->next;
1110
0
          rdm->next = backref;
1111
0
          open = demangle_path_maybe_open_generics (rdm);
1112
0
          rdm->next = old_next;
1113
0
        }
1114
0
    }
1115
0
  else if (eat (rdm, 'I'))
1116
0
    {
1117
0
      demangle_path (rdm, 0);
1118
0
      PRINT ("<");
1119
0
      open = 1;
1120
0
      for (i = 0; !rdm->errored && !eat (rdm, 'E'); i++)
1121
0
        {
1122
0
          if (i > 0)
1123
0
            PRINT (", ");
1124
0
          demangle_generic_arg (rdm);
1125
0
        }
1126
0
    }
1127
0
  else
1128
0
    demangle_path (rdm, 0);
1129
1130
0
 end_of_func:
1131
0
  if (rdm->recursion != RUST_NO_RECURSION_LIMIT)
1132
0
    -- rdm->recursion;
1133
1134
0
  return open;
1135
0
}
1136
1137
static void
1138
demangle_dyn_trait (struct rust_demangler *rdm)
1139
0
{
1140
0
  int open;
1141
0
  struct rust_mangled_ident name;
1142
1143
0
  if (rdm->errored)
1144
0
    return;
1145
1146
0
  open = demangle_path_maybe_open_generics (rdm);
1147
1148
0
  while (eat (rdm, 'p'))
1149
0
    {
1150
0
      if (!open)
1151
0
        PRINT ("<");
1152
0
      else
1153
0
        PRINT (", ");
1154
0
      open = 1;
1155
1156
0
      name = parse_ident (rdm);
1157
0
      print_ident (rdm, name);
1158
0
      PRINT (" = ");
1159
0
      demangle_type (rdm);
1160
0
    }
1161
1162
0
  if (open)
1163
0
    PRINT (">");
1164
0
}
1165
1166
static void
1167
demangle_const (struct rust_demangler *rdm)
1168
0
{
1169
0
  char ty_tag;
1170
0
  size_t old_next, backref;
1171
1172
0
  if (rdm->errored)
1173
0
    return;
1174
1175
0
  if (rdm->recursion != RUST_NO_RECURSION_LIMIT)
1176
0
    {
1177
0
      ++ rdm->recursion;
1178
0
      if (rdm->recursion > RUST_MAX_RECURSION_COUNT)
1179
  /* FIXME: There ought to be a way to report
1180
     that the recursion limit has been reached.  */
1181
0
  goto fail_return;
1182
0
    }
1183
1184
0
  if (eat (rdm, 'B'))
1185
0
    {
1186
0
      backref = parse_integer_62 (rdm);
1187
0
      if (!rdm->skipping_printing)
1188
0
        {
1189
0
          old_next = rdm->next;
1190
0
          rdm->next = backref;
1191
0
          demangle_const (rdm);
1192
0
          rdm->next = old_next;
1193
0
        }
1194
0
      goto pass_return;
1195
0
    }
1196
1197
0
  ty_tag = next (rdm);
1198
0
  switch (ty_tag)
1199
0
    {
1200
    /* Placeholder. */
1201
0
    case 'p':
1202
0
      PRINT ("_");
1203
0
      goto pass_return;
1204
1205
    /* Unsigned integer types. */
1206
0
    case 'h':
1207
0
    case 't':
1208
0
    case 'm':
1209
0
    case 'y':
1210
0
    case 'o':
1211
0
    case 'j':
1212
0
      demangle_const_uint (rdm);
1213
0
      break;
1214
1215
    /* Signed integer types. */
1216
0
    case 'a':
1217
0
    case 's':
1218
0
    case 'l':
1219
0
    case 'x':
1220
0
    case 'n':
1221
0
    case 'i':
1222
0
      demangle_const_int (rdm);
1223
0
      break;
1224
1225
    /* Boolean. */
1226
0
    case 'b':
1227
0
      demangle_const_bool (rdm);
1228
0
      break;
1229
1230
    /* Character. */
1231
0
    case 'c':
1232
0
      demangle_const_char (rdm);
1233
0
      break;
1234
1235
0
    default:
1236
0
      goto fail_return;
1237
0
    }
1238
1239
0
  if (!rdm->errored && rdm->verbose)
1240
0
    {
1241
0
      PRINT (": ");
1242
0
      PRINT (basic_type (ty_tag));
1243
0
    }
1244
0
  goto pass_return;
1245
1246
0
 fail_return:
1247
0
  rdm->errored = 1;
1248
0
 pass_return:
1249
0
  if (rdm->recursion != RUST_NO_RECURSION_LIMIT)
1250
0
    -- rdm->recursion;
1251
0
}
1252
1253
static void
1254
demangle_const_uint (struct rust_demangler *rdm)
1255
0
{
1256
0
  size_t hex_len;
1257
0
  uint64_t value;
1258
1259
0
  if (rdm->errored)
1260
0
    return;
1261
1262
0
  hex_len = parse_hex_nibbles (rdm, &value);
1263
1264
0
  if (hex_len > 16)
1265
0
    {
1266
      /* Print anything that doesn't fit in `uint64_t` verbatim. */
1267
0
      PRINT ("0x");
1268
0
      print_str (rdm, rdm->sym + (rdm->next - hex_len), hex_len);
1269
0
    }
1270
0
  else if (hex_len > 0)
1271
0
    print_uint64 (rdm, value);
1272
0
  else
1273
0
    rdm->errored = 1;
1274
0
}
1275
1276
static void
1277
demangle_const_int (struct rust_demangler *rdm)
1278
0
{
1279
0
  if (eat (rdm, 'n'))
1280
0
    PRINT ("-");
1281
0
  demangle_const_uint (rdm);
1282
0
}
1283
1284
static void
1285
demangle_const_bool (struct rust_demangler *rdm)
1286
0
{
1287
0
  uint64_t value;
1288
1289
0
  if (parse_hex_nibbles (rdm, &value) != 1)
1290
0
    {
1291
0
      rdm->errored = 1;
1292
0
      return;
1293
0
    }
1294
1295
0
  if (value == 0)
1296
0
    PRINT ("false");
1297
0
  else if (value == 1)
1298
0
    PRINT ("true");
1299
0
  else
1300
0
    rdm->errored = 1;
1301
0
}
1302
1303
static void
1304
demangle_const_char (struct rust_demangler *rdm)
1305
0
{
1306
0
  size_t hex_len;
1307
0
  uint64_t value;
1308
1309
0
  hex_len = parse_hex_nibbles (rdm, &value);
1310
1311
0
  if (hex_len == 0 || hex_len > 8)
1312
0
    {
1313
0
      rdm->errored = 1;
1314
0
      return;
1315
0
    }
1316
1317
  /* Match Rust's character "debug" output as best as we can. */
1318
0
  PRINT ("'");
1319
0
  if (value == '\t')
1320
0
    PRINT ("\\t");
1321
0
  else if (value == '\r')
1322
0
    PRINT ("\\r");
1323
0
  else if (value == '\n')
1324
0
    PRINT ("\\n");
1325
0
  else if (value > ' ' && value < '~')
1326
0
    {
1327
      /* Rust also considers many non-ASCII codepoints to be printable, but
1328
   that logic is not easily ported to C. */
1329
0
      char c = value;
1330
0
      print_str (rdm, &c, 1);
1331
0
    }
1332
0
  else
1333
0
    {
1334
0
      PRINT ("\\u{");
1335
0
      print_uint64_hex (rdm, value);
1336
0
      PRINT ("}");
1337
0
    }
1338
0
  PRINT ("'");
1339
0
}
1340
1341
/* A legacy hash is the prefix "h" followed by 16 lowercase hex digits.
1342
   The hex digits must contain at least 5 distinct digits. */
1343
static int
1344
is_legacy_prefixed_hash (struct rust_mangled_ident ident)
1345
0
{
1346
0
  uint16_t seen;
1347
0
  int nibble;
1348
0
  size_t i, count;
1349
1350
0
  if (ident.ascii_len != 17 || ident.ascii[0] != 'h')
1351
0
    return 0;
1352
1353
0
  seen = 0;
1354
0
  for (i = 0; i < 16; i++)
1355
0
    {
1356
0
      nibble = decode_lower_hex_nibble (ident.ascii[1 + i]);
1357
0
      if (nibble < 0)
1358
0
        return 0;
1359
0
      seen |= (uint16_t)1 << nibble;
1360
0
    }
1361
1362
  /* Count how many distinct digits were seen. */
1363
0
  count = 0;
1364
0
  while (seen)
1365
0
    {
1366
0
      if (seen & 1)
1367
0
        count++;
1368
0
      seen >>= 1;
1369
0
    }
1370
1371
0
  return count >= 5;
1372
0
}
1373
1374
int
1375
rust_demangle_callback (const char *mangled, int options,
1376
                        demangle_callbackref callback, void *opaque)
1377
0
{
1378
0
  const char *p;
1379
0
  struct rust_demangler rdm;
1380
0
  struct rust_mangled_ident ident;
1381
1382
0
  rdm.sym = mangled;
1383
0
  rdm.sym_len = 0;
1384
1385
0
  rdm.callback_opaque = opaque;
1386
0
  rdm.callback = callback;
1387
1388
0
  rdm.next = 0;
1389
0
  rdm.errored = 0;
1390
0
  rdm.skipping_printing = 0;
1391
0
  rdm.verbose = (options & DMGL_VERBOSE) != 0;
1392
0
  rdm.version = 0;
1393
0
  rdm.recursion = (options & DMGL_NO_RECURSE_LIMIT) ? RUST_NO_RECURSION_LIMIT : 0;
1394
0
  rdm.bound_lifetime_depth = 0;
1395
1396
  /* Rust symbols always start with _R (v0) or _ZN (legacy). */
1397
0
  if (rdm.sym[0] == '_' && rdm.sym[1] == 'R')
1398
0
    rdm.sym += 2;
1399
0
  else if (rdm.sym[0] == '_' && rdm.sym[1] == 'Z' && rdm.sym[2] == 'N')
1400
0
    {
1401
0
      rdm.sym += 3;
1402
0
      rdm.version = -1;
1403
0
    }
1404
0
  else
1405
0
    return 0;
1406
1407
  /* Paths (v0) always start with uppercase characters. */
1408
0
  if (rdm.version != -1 && !ISUPPER (rdm.sym[0]))
1409
0
    return 0;
1410
1411
  /* Rust symbols (v0) use only [_0-9a-zA-Z] characters. */
1412
0
  for (p = rdm.sym; *p; p++)
1413
0
    {
1414
      /* Rust v0 symbols can have '.' suffixes, ignore those.  */
1415
0
      if (rdm.version == 0 && *p == '.')
1416
0
        break;
1417
1418
0
      rdm.sym_len++;
1419
1420
0
      if (*p == '_' || ISALNUM (*p))
1421
0
        continue;
1422
1423
      /* Legacy Rust symbols can also contain [.:$] characters.
1424
         Or @ in the .suffix (which will be skipped, see below). */
1425
0
      if (rdm.version == -1 && (*p == '$' || *p == '.' || *p == ':'
1426
0
                                || *p == '@'))
1427
0
        continue;
1428
1429
0
      return 0;
1430
0
    }
1431
1432
  /* Legacy Rust symbols need to be handled separately. */
1433
0
  if (rdm.version == -1)
1434
0
    {
1435
      /* Legacy Rust symbols always end with E.  But can be followed by a
1436
         .suffix (which we want to ignore).  */
1437
0
      int dot_suffix = 1;
1438
0
      while (rdm.sym_len > 0 &&
1439
0
             !(dot_suffix && rdm.sym[rdm.sym_len - 1] == 'E'))
1440
0
        {
1441
0
          dot_suffix = rdm.sym[rdm.sym_len - 1] == '.';
1442
0
          rdm.sym_len--;
1443
0
        }
1444
1445
0
      if (!(rdm.sym_len > 0 && rdm.sym[rdm.sym_len - 1] == 'E'))
1446
0
        return 0;
1447
0
      rdm.sym_len--;
1448
1449
      /* Legacy Rust symbols also always end with a path segment
1450
         that encodes a 16 hex digit hash, i.e. '17h[a-f0-9]{16}'.
1451
         This early check, before any parse_ident calls, should
1452
         quickly filter out most C++ symbols unrelated to Rust. */
1453
0
      if (!(rdm.sym_len > 19
1454
0
            && !memcmp (&rdm.sym[rdm.sym_len - 19], "17h", 3)))
1455
0
        return 0;
1456
1457
0
      do
1458
0
        {
1459
0
          ident = parse_ident (&rdm);
1460
0
          if (rdm.errored || !ident.ascii)
1461
0
            return 0;
1462
0
        }
1463
0
      while (rdm.next < rdm.sym_len);
1464
1465
      /* The last path segment should be the hash. */
1466
0
      if (!is_legacy_prefixed_hash (ident))
1467
0
        return 0;
1468
1469
      /* Reset the state for a second pass, to print the symbol. */
1470
0
      rdm.next = 0;
1471
0
      if (!rdm.verbose && rdm.sym_len > 19)
1472
0
        {
1473
          /* Hide the last segment, containing the hash, if not verbose. */
1474
0
          rdm.sym_len -= 19;
1475
0
        }
1476
1477
0
      do
1478
0
        {
1479
0
          if (rdm.next > 0)
1480
0
            print_str (&rdm, "::", 2);
1481
1482
0
          ident = parse_ident (&rdm);
1483
0
          print_ident (&rdm, ident);
1484
0
        }
1485
0
      while (rdm.next < rdm.sym_len);
1486
0
    }
1487
0
  else
1488
0
    {
1489
0
      demangle_path (&rdm, 1);
1490
1491
      /* Skip instantiating crate. */
1492
0
      if (!rdm.errored && rdm.next < rdm.sym_len)
1493
0
        {
1494
0
          rdm.skipping_printing = 1;
1495
0
          demangle_path (&rdm, 0);
1496
0
        }
1497
1498
      /* It's an error to not reach the end. */
1499
0
      rdm.errored |= rdm.next != rdm.sym_len;
1500
0
    }
1501
1502
0
  return !rdm.errored;
1503
0
}
1504
1505
/* Growable string buffers. */
1506
struct str_buf
1507
{
1508
  char *ptr;
1509
  size_t len;
1510
  size_t cap;
1511
  int errored;
1512
};
1513
1514
static void
1515
str_buf_reserve (struct str_buf *buf, size_t extra)
1516
0
{
1517
0
  size_t available, min_new_cap, new_cap;
1518
0
  char *new_ptr;
1519
1520
  /* Allocation failed before. */
1521
0
  if (buf->errored)
1522
0
    return;
1523
1524
0
  available = buf->cap - buf->len;
1525
1526
0
  if (extra <= available)
1527
0
    return;
1528
1529
0
  min_new_cap = buf->cap + (extra - available);
1530
1531
  /* Check for overflows. */
1532
0
  if (min_new_cap < buf->cap)
1533
0
    {
1534
0
      buf->errored = 1;
1535
0
      return;
1536
0
    }
1537
1538
0
  new_cap = buf->cap;
1539
1540
0
  if (new_cap == 0)
1541
0
    new_cap = 4;
1542
1543
  /* Double capacity until sufficiently large. */
1544
0
  while (new_cap < min_new_cap)
1545
0
    {
1546
0
      new_cap *= 2;
1547
1548
      /* Check for overflows. */
1549
0
      if (new_cap < buf->cap)
1550
0
        {
1551
0
          buf->errored = 1;
1552
0
          return;
1553
0
        }
1554
0
    }
1555
1556
0
  new_ptr = (char *)realloc (buf->ptr, new_cap);
1557
0
  if (new_ptr == NULL)
1558
0
    {
1559
0
      free (buf->ptr);
1560
0
      buf->ptr = NULL;
1561
0
      buf->len = 0;
1562
0
      buf->cap = 0;
1563
0
      buf->errored = 1;
1564
0
    }
1565
0
  else
1566
0
    {
1567
0
      buf->ptr = new_ptr;
1568
0
      buf->cap = new_cap;
1569
0
    }
1570
0
}
1571
1572
static void
1573
str_buf_append (struct str_buf *buf, const char *data, size_t len)
1574
0
{
1575
0
  str_buf_reserve (buf, len);
1576
0
  if (buf->errored)
1577
0
    return;
1578
1579
0
  memcpy (buf->ptr + buf->len, data, len);
1580
0
  buf->len += len;
1581
0
}
1582
1583
static void
1584
str_buf_demangle_callback (const char *data, size_t len, void *opaque)
1585
0
{
1586
0
  str_buf_append ((struct str_buf *)opaque, data, len);
1587
0
}
1588
1589
char *
1590
rust_demangle (const char *mangled, int options)
1591
0
{
1592
0
  struct str_buf out;
1593
0
  int success;
1594
1595
0
  out.ptr = NULL;
1596
0
  out.len = 0;
1597
0
  out.cap = 0;
1598
0
  out.errored = 0;
1599
1600
0
  success = rust_demangle_callback (mangled, options,
1601
0
                                    str_buf_demangle_callback, &out);
1602
1603
0
  if (!success)
1604
0
    {
1605
0
      free (out.ptr);
1606
0
      return NULL;
1607
0
    }
1608
1609
0
  str_buf_append (&out, "\0", 1);
1610
0
  return out.ptr;
1611
0
}