Coverage Report

Created: 2026-08-17 07:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gettext/gettext-tools/src/format-sh-printf.c
Line
Count
Source
1
/* Shell printf format strings.
2
   Copyright (C) 2001-2026 Free Software Foundation, Inc.
3
4
   This program is free software: you can redistribute it and/or modify
5
   it under the terms of the GNU General Public License as published by
6
   the Free Software Foundation; either version 3 of the License, or
7
   (at your option) any later version.
8
9
   This program is distributed in the hope that it will be useful,
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
   GNU General Public License for more details.
13
14
   You should have received a copy of the GNU General Public License
15
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16
17
/* Written by Bruno Haible.  */
18
19
#include <config.h>
20
21
#include <stdbool.h>
22
#include <stdlib.h>
23
24
#include "format.h"
25
#include "attribute.h"
26
#include "c-ctype.h"
27
#include "xalloc.h"
28
#include "xvasprintf.h"
29
#include "format-invalid.h"
30
#include "gettext.h"
31
32
0
#define _(str) gettext (str)
33
34
/* Shell printf format strings are described in
35
     * POSIX:
36
       <https://pubs.opengroup.org/onlinepubs/9799919799/utilities/printf.html>
37
       <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap05.html#tag_05>
38
     * The GNU coreutils documentation:
39
       <https://www.gnu.org/software/coreutils/manual/html_node/printf-invocation.html>
40
     * The GNU bash documentation:
41
       <https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-printf>
42
43
   The format string consists of
44
     - plain text,
45
     - directives, that start with '%',
46
     - escape sequences, that start with a backslash and don't contain '%'.
47
48
   The set of supported directives and escape sequences is documented in gettext.texi.
49
50
   A directive
51
   - starts with '%' or '%m$' where m is a positive integer,
52
   - is optionally followed by any of the characters '#', '0', '-', ' ', '+',
53
     each of which acts as a flag,
54
   - is optionally followed by a width specification: a nonempty digit sequence,
55
     [not in POSIX: '*' (reads an argument) or '*m$']
56
   - is optionally followed by '.' and a precision specification: an optional
57
     nonempty digit sequence,
58
     [not in POSIX: '*' (reads an argument) or '*m$']
59
   - [not in POSIX: is optionally followed by a size specifier, one of
60
      'hh' 'h' 'l' 'll' 'L' 'q' 'j' 'z' 't']
61
   - is finished by a specifier
62
       - 'c', that needs a character argument,
63
       - 's', that needs a string argument,
64
       - 'i', 'd', that need a signed integer argument,
65
       - 'u', 'o', 'x', 'X', that need an unsigned integer argument,
66
       - [optional in POSIX, but supported here:] 'e', 'E', 'f', 'F', 'g', 'G',
67
         'a', 'A', that need a floating-point argument.
68
   Some flag+specifier combinations are invalid:
69
     - The '#' flag with the specifiers 'c', 's', 'i', 'd', 'u'.
70
     - The '0' flag with the specifiers 'c', 's'.
71
   Additionally there is the directive '%%', which takes no argument.
72
   Numbered ('%m$' or '*m$') and unnumbered argument specifications cannot
73
   be used in the same string.
74
75
   The valid escape sequences are:
76
     \\ \a \b \f \n \r \t \v
77
     \nnn with 1 to 3 octal digits n
78
     [not in POSIX: \c \xnn \unnnn \Unnnnnnnn]
79
 */
80
81
enum format_arg_type
82
{
83
  FAT_NONE,
84
  FAT_CHARACTER,
85
  FAT_STRING,
86
  FAT_INTEGER,
87
  FAT_UNSIGNED_INTEGER,
88
  FAT_FLOAT
89
};
90
91
struct numbered_arg
92
{
93
  size_t number;
94
  enum format_arg_type type;
95
};
96
97
struct spec
98
{
99
  size_t directives;
100
  /* We consider a directive as "likely intentional" if it does not contain a
101
     space.  This prevents xgettext from flagging strings like "100% complete"
102
     as 'sh-printf-format' if they don't occur in a context that requires a
103
     format string.  */
104
  size_t likely_intentional_directives;
105
  size_t numbered_arg_count;
106
  struct numbered_arg *numbered
107
    COUNTED_BY (numbered_arg_count);
108
};
109
110
111
static int
112
numbered_arg_compare (const void *p1, const void *p2)
113
0
{
114
0
  size_t n1 = ((const struct numbered_arg *) p1)->number;
115
0
  size_t n2 = ((const struct numbered_arg *) p2)->number;
116
117
0
  return (n1 > n2 ? 1 : n1 < n2 ? -1 : 0);
118
0
}
119
120
static void *
121
format_parse (const char *format, bool translated, char *fdi,
122
              char **invalid_reason)
123
0
{
124
0
  const char *const format_start = format;
125
126
0
  struct spec spec;
127
0
  spec.directives = 0;
128
0
  spec.likely_intentional_directives = 0;
129
0
  spec.numbered_arg_count = 0;
130
0
  spec.numbered = NULL;
131
0
  size_t numbered_allocated = 0;
132
0
  size_t unnumbered_arg_count = 0;
133
0
  struct numbered_arg *unnumbered = NULL;
134
135
0
  for (; *format != '\0';)
136
    /* Invariant: spec.numbered_arg_count == 0 || unnumbered_arg_count == 0.  */
137
0
    if (*format == '%')
138
0
      {
139
        /* A directive.  */
140
0
        FDI_SET (format, FMTDIR_START);
141
0
        format++;
142
0
        spec.directives++;
143
0
        bool likely_intentional = true;
144
145
0
        if (*format != '%')
146
0
          {
147
0
            size_t number = 0;
148
0
            if (c_isdigit (*format))
149
0
              {
150
0
                const char *f = format;
151
0
                size_t m = 0;
152
153
0
                do
154
0
                  {
155
0
                    m = 10 * m + (*f - '0');
156
0
                    f++;
157
0
                  }
158
0
                while (c_isdigit (*f));
159
160
0
                if (*f == '$')
161
0
                  {
162
0
                    if (m == 0)
163
0
                      {
164
0
                        *invalid_reason = INVALID_ARGNO_0 (spec.directives);
165
0
                        FDI_SET (f, FMTDIR_ERROR);
166
0
                        goto bad_format;
167
0
                      }
168
0
                    number = m;
169
0
                    format = ++f;
170
0
                  }
171
0
              }
172
173
            /* Parse flags.  */
174
0
            bool have_hash_flag = false;
175
0
            bool have_zero_flag = false;
176
0
            while (*format == ' ' || *format == '+' || *format == '-'
177
0
                   || *format == '#' || *format == '0')
178
0
              {
179
0
                if (*format == ' ')
180
0
                  likely_intentional = false;
181
0
                if (*format == '#')
182
0
                  have_hash_flag = true;
183
0
                if (*format == '0')
184
0
                  have_zero_flag = true;
185
0
                format++;
186
0
              }
187
188
            /* Parse width.  */
189
0
            if (c_isdigit (*format))
190
0
              {
191
0
                do format++; while (c_isdigit (*format));
192
0
              }
193
194
            /* Parse precision.  */
195
0
            if (*format == '.')
196
0
              {
197
0
                format++;
198
199
0
                while (c_isdigit (*format))
200
0
                  format++;
201
0
              }
202
203
0
            enum format_arg_type type;
204
0
            switch (*format)
205
0
              {
206
0
              case 'c':
207
0
                type = FAT_CHARACTER;
208
0
                break;
209
0
              case 's':
210
0
                type = FAT_STRING;
211
0
                break;
212
0
              case 'i': case 'd':
213
0
                type = FAT_INTEGER;
214
0
                break;
215
0
              case 'u': case 'o': case 'x': case 'X':
216
0
                type = FAT_UNSIGNED_INTEGER;
217
0
                break;
218
0
              case 'e': case 'E': case 'f': case 'F': case 'g': case 'G':
219
0
              case 'a': case 'A':
220
0
                type = FAT_FLOAT;
221
0
                break;
222
0
              default:
223
0
                if (*format == '\0')
224
0
                  {
225
0
                    *invalid_reason = INVALID_UNTERMINATED_DIRECTIVE ();
226
0
                    FDI_SET (format - 1, FMTDIR_ERROR);
227
0
                  }
228
0
                else
229
0
                  {
230
0
                    *invalid_reason =
231
0
                      INVALID_CONVERSION_SPECIFIER (spec.directives, *format);
232
0
                    FDI_SET (format, FMTDIR_ERROR);
233
0
                  }
234
0
                goto bad_format;
235
0
              }
236
237
0
            if (have_hash_flag
238
0
                && (*format == 'c' || *format == 's'
239
0
                    || *format == 'i' || *format == 'd' || *format == 'u'))
240
0
              {
241
0
                *invalid_reason =
242
0
                  INVALID_FLAG_FOR (spec.directives, '#', *format);
243
0
                FDI_SET (format, FMTDIR_ERROR);
244
0
                goto bad_format;
245
0
              }
246
0
            if (have_zero_flag && (*format == 'c' || *format == 's'))
247
0
              {
248
0
                *invalid_reason =
249
0
                  INVALID_FLAG_FOR (spec.directives, '0', *format);
250
0
                FDI_SET (format, FMTDIR_ERROR);
251
0
                goto bad_format;
252
0
              }
253
254
0
            if (number)
255
0
              {
256
                /* Numbered argument.  */
257
258
                /* Numbered and unnumbered specifications are exclusive.  */
259
0
                if (unnumbered_arg_count > 0)
260
0
                  {
261
0
                    *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
262
0
                    FDI_SET (format, FMTDIR_ERROR);
263
0
                    goto bad_format;
264
0
                  }
265
266
0
                if (numbered_allocated == spec.numbered_arg_count)
267
0
                  {
268
0
                    numbered_allocated = 2 * numbered_allocated + 1;
269
0
                    spec.numbered = (struct numbered_arg *) xrealloc (spec.numbered, numbered_allocated * sizeof (struct numbered_arg));
270
0
                  }
271
0
                size_t numbered_index = spec.numbered_arg_count++;
272
0
                spec.numbered[numbered_index].number = number;
273
0
                spec.numbered[numbered_index].type = type;
274
0
              }
275
0
            else
276
0
              {
277
                /* Unnumbered argument.  */
278
279
                /* Numbered and unnumbered specifications are exclusive.  */
280
0
                if (spec.numbered_arg_count > 0)
281
0
                  {
282
0
                    *invalid_reason = INVALID_MIXES_NUMBERED_UNNUMBERED ();
283
0
                    FDI_SET (format, FMTDIR_ERROR);
284
0
                    goto bad_format;
285
0
                  }
286
287
0
                if (numbered_allocated == unnumbered_arg_count)
288
0
                  {
289
0
                    numbered_allocated = 2 * numbered_allocated + 1;
290
0
                    unnumbered = (struct numbered_arg *) xrealloc (unnumbered, numbered_allocated * sizeof (struct numbered_arg));
291
0
                  }
292
0
                size_t unnumbered_index = unnumbered_arg_count++;
293
0
                unnumbered[unnumbered_index].number = unnumbered_index + 1;
294
0
                unnumbered[unnumbered_index].type = type;
295
0
              }
296
0
          }
297
298
0
        if (likely_intentional)
299
0
          spec.likely_intentional_directives++;
300
0
        FDI_SET (format, FMTDIR_END);
301
302
0
        format++;
303
0
      }
304
0
    else if (*format == '\\')
305
0
      {
306
        /* An escape sequence.  */
307
0
        FDI_SET (format, FMTDIR_START);
308
0
        format++;
309
310
0
        switch (*format)
311
0
          {
312
0
          case '\\':
313
0
          case 'a':
314
0
          case 'b':
315
0
          case 'f':
316
0
          case 'n':
317
0
          case 'r':
318
0
          case 't':
319
0
          case 'v':
320
0
            format++;
321
0
            break;
322
323
0
          case '0': case '1': case '2': case '3': case '4': case '5': case '6':
324
0
          case '7':
325
0
            format++;
326
0
            if (*format >= '0' && *format <= '7')
327
0
              {
328
0
                format++;
329
0
                if (*format >= '0' && *format <= '7')
330
0
                  format++;
331
0
              }
332
0
            break;
333
334
0
          default:
335
0
            if (*format == '\0')
336
0
              {
337
0
                *invalid_reason =
338
0
                  xstrdup (_("The string ends in the middle of an escape sequence."));
339
0
                FDI_SET (format - 1, FMTDIR_ERROR);
340
0
              }
341
0
            else
342
0
              {
343
0
                *invalid_reason =
344
0
                  (c_isprint (*format)
345
0
                   ? ((*format == 'c'
346
0
                       || *format == 'x'
347
0
                       || *format == 'u' || *format == 'U')
348
0
                      ? xasprintf (_("The escape sequence '%c%c' is unsupported (not in POSIX)."), '\\', *format)
349
0
                      : xasprintf (_("The escape sequence '%c%c' is invalid."), '\\', *format))
350
0
                   : xstrdup (_("This escape sequence is invalid.")));
351
0
                FDI_SET (format, FMTDIR_ERROR);
352
0
              }
353
0
            goto bad_format;
354
0
          }
355
0
        FDI_SET (format - 1, FMTDIR_END);
356
0
      }
357
0
    else
358
0
      format++;
359
360
  /* Convert the unnumbered argument array to numbered arguments.  */
361
0
  if (unnumbered_arg_count > 0)
362
0
    {
363
0
      spec.numbered = unnumbered;
364
0
      spec.numbered_arg_count = unnumbered_arg_count;
365
0
    }
366
  /* Sort the numbered argument array, and eliminate duplicates.  */
367
0
  else if (spec.numbered_arg_count > 1)
368
0
    {
369
0
      qsort (spec.numbered, spec.numbered_arg_count,
370
0
             sizeof (struct numbered_arg), numbered_arg_compare);
371
372
      /* Remove duplicates: Copy from i to j, keeping 0 <= j <= i.  */
373
0
      bool err = false;
374
0
      size_t i, j;
375
0
      for (i = j = 0; i < spec.numbered_arg_count; i++)
376
0
        if (j > 0 && spec.numbered[i].number == spec.numbered[j-1].number)
377
0
          {
378
0
            enum format_arg_type type1 = spec.numbered[i].type;
379
0
            enum format_arg_type type2 = spec.numbered[j-1].type;
380
381
0
            enum format_arg_type type_both;
382
0
            if (type1 == type2)
383
0
              type_both = type1;
384
0
            else
385
0
              {
386
                /* Incompatible types.  */
387
0
                type_both = FAT_NONE;
388
0
                if (!err)
389
0
                  *invalid_reason =
390
0
                    INVALID_INCOMPATIBLE_ARG_TYPES (spec.numbered[i].number);
391
0
                err = true;
392
0
              }
393
394
0
            spec.numbered[j-1].type = type_both;
395
0
          }
396
0
        else
397
0
          {
398
0
            if (j < i)
399
0
              {
400
0
                spec.numbered[j].number = spec.numbered[i].number;
401
0
                spec.numbered[j].type = spec.numbered[i].type;
402
0
              }
403
0
            j++;
404
0
          }
405
0
      spec.numbered_arg_count = j;
406
0
      if (err)
407
        /* *invalid_reason has already been set above.  */
408
0
        goto bad_format;
409
0
    }
410
411
0
  struct spec *result = XMALLOC (struct spec);
412
0
  *result = spec;
413
0
  return result;
414
415
0
 bad_format:
416
0
  if (unnumbered != NULL)
417
0
    free (unnumbered);
418
0
  if (spec.numbered != NULL)
419
0
    free (spec.numbered);
420
0
  return NULL;
421
0
}
422
423
static void
424
format_free (void *descr)
425
0
{
426
0
  struct spec *spec = (struct spec *) descr;
427
428
0
  if (spec->numbered != NULL)
429
0
    free (spec->numbered);
430
0
  free (spec);
431
0
}
432
433
static int
434
format_get_number_of_directives (void *descr)
435
0
{
436
0
  struct spec *spec = (struct spec *) descr;
437
438
0
  return spec->directives;
439
0
}
440
441
static bool
442
format_is_unlikely_intentional (void *descr)
443
0
{
444
0
  struct spec *spec = (struct spec *) descr;
445
446
0
  return spec->likely_intentional_directives == 0;
447
0
}
448
449
static bool
450
format_check (void *msgid_descr, void *msgstr_descr, bool equality,
451
              formatstring_error_logger_t error_logger, void *error_logger_data,
452
              const char *pretty_msgid, const char *pretty_msgstr)
453
0
{
454
0
  struct spec *spec1 = (struct spec *) msgid_descr;
455
0
  struct spec *spec2 = (struct spec *) msgstr_descr;
456
0
  bool err = false;
457
458
0
  if (spec1->numbered_arg_count + spec2->numbered_arg_count > 0)
459
0
    {
460
0
      size_t n1 = spec1->numbered_arg_count;
461
0
      size_t n2 = spec2->numbered_arg_count;
462
463
      /* Check that the argument numbers are the same.
464
         Both arrays are sorted.  We search for the first difference.  */
465
0
      {
466
0
        size_t i, j;
467
0
        for (i = 0, j = 0; i < n1 || j < n2; )
468
0
          {
469
0
            int cmp = (i >= n1 ? 1 :
470
0
                       j >= n2 ? -1 :
471
0
                       spec1->numbered[i].number > spec2->numbered[j].number ? 1 :
472
0
                       spec1->numbered[i].number < spec2->numbered[j].number ? -1 :
473
0
                       0);
474
475
0
            if (cmp > 0)
476
0
              {
477
0
                if (error_logger)
478
0
                  error_logger (error_logger_data,
479
0
                                _("a format specification for argument %zu, as in '%s', doesn't exist in '%s'"),
480
0
                                spec2->numbered[j].number, pretty_msgstr,
481
0
                                pretty_msgid);
482
0
                err = true;
483
0
                break;
484
0
              }
485
0
            else if (cmp < 0)
486
0
              {
487
0
                if (equality)
488
0
                  {
489
0
                    if (error_logger)
490
0
                      error_logger (error_logger_data,
491
0
                                    _("a format specification for argument %zu doesn't exist in '%s'"),
492
0
                                    spec1->numbered[i].number, pretty_msgstr);
493
0
                    err = true;
494
0
                    break;
495
0
                  }
496
0
                else
497
0
                  i++;
498
0
              }
499
0
            else
500
0
              j++, i++;
501
0
          }
502
0
      }
503
      /* Check the argument types are the same.  */
504
0
      if (!err)
505
0
        {
506
0
          size_t i, j;
507
0
          for (i = 0, j = 0; j < n2; )
508
0
            {
509
0
              if (spec1->numbered[i].number == spec2->numbered[j].number)
510
0
                {
511
0
                  if (spec1->numbered[i].type != spec2->numbered[j].type)
512
0
                    {
513
0
                      if (error_logger)
514
0
                        error_logger (error_logger_data,
515
0
                                      _("format specifications in '%s' and '%s' for argument %zu are not the same"),
516
0
                                      pretty_msgid, pretty_msgstr,
517
0
                                      spec2->numbered[j].number);
518
0
                      err = true;
519
0
                      break;
520
0
                    }
521
0
                  j++, i++;
522
0
                }
523
0
              else
524
0
                i++;
525
0
            }
526
0
        }
527
0
    }
528
529
0
  return err;
530
0
}
531
532
533
struct formatstring_parser formatstring_sh_printf =
534
{
535
  format_parse,
536
  format_free,
537
  format_get_number_of_directives,
538
  format_is_unlikely_intentional,
539
  format_check
540
};
541
542
543
#ifdef TEST
544
545
/* Test program: Print the argument list specification returned by
546
   format_parse for strings read from standard input.  */
547
548
#include <stdio.h>
549
550
static void
551
format_print (void *descr)
552
{
553
  struct spec *spec = (struct spec *) descr;
554
555
  if (spec == NULL)
556
    {
557
      printf ("INVALID");
558
      return;
559
    }
560
561
  printf ("(");
562
  size_t last = 1;
563
  for (size_t i = 0; i < spec->numbered_arg_count; i++)
564
    {
565
      size_t number = spec->numbered[i].number;
566
567
      if (i > 0)
568
        printf (" ");
569
      if (number < last)
570
        abort ();
571
      for (; last < number; last++)
572
        printf ("_ ");
573
      switch (spec->numbered[i].type)
574
        {
575
        case FAT_CHARACTER:
576
          printf ("c");
577
          break;
578
        case FAT_STRING:
579
          printf ("s");
580
          break;
581
        case FAT_INTEGER:
582
          printf ("i");
583
          break;
584
        case FAT_UNSIGNED_INTEGER:
585
          printf ("[unsigned]i");
586
          break;
587
        case FAT_FLOAT:
588
          printf ("f");
589
          break;
590
        default:
591
          abort ();
592
        }
593
      last = number + 1;
594
    }
595
  printf (")");
596
}
597
598
int
599
main ()
600
{
601
  for (;;)
602
    {
603
      char *line = NULL;
604
      size_t line_size = 0;
605
      int line_len = getline (&line, &line_size, stdin);
606
      if (line_len < 0)
607
        break;
608
      if (line_len > 0 && line[line_len - 1] == '\n')
609
        line[--line_len] = '\0';
610
611
      char *invalid_reason = NULL;
612
      void *descr = format_parse (line, false, NULL, &invalid_reason);
613
614
      format_print (descr);
615
      printf ("\n");
616
      if (descr == NULL)
617
        printf ("%s\n", invalid_reason);
618
619
      free (invalid_reason);
620
      free (line);
621
    }
622
623
  return 0;
624
}
625
626
/*
627
 * For Emacs M-x compile
628
 * Local Variables:
629
 * compile-command: "/bin/sh ../libtool --tag=CC --mode=link gcc -o a.out -static -O -g -Wall -I.. -I../gnulib-lib -I../../gettext-runtime/intl -DTEST format-sh-printf.c ../gnulib-lib/libgettextlib.la"
630
 * End:
631
 */
632
633
#endif /* TEST */