Coverage Report

Created: 2026-01-25 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gettext-0.26/gettext-runtime/intl/eval-plural.h
Line
Count
Source
1
/* Plural expression evaluation.
2
   Copyright (C) 2000-2023 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 Lesser General Public License as published by
6
   the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
13
14
   You should have received a copy of the GNU Lesser General Public License
15
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16
17
#ifndef STATIC
18
#define STATIC static
19
#endif
20
21
/* While the bison parser is able to support expressions of a maximum depth
22
   of YYMAXDEPTH = 10000, the runtime evaluation of a parsed plural expression
23
   has a smaller maximum recursion depth.
24
   If we did not limit the recursion depth, a program that just invokes
25
   ngettext() on a thread other than the main thread could get a crash by
26
   stack overflow in the following circumstances:
27
     - On systems with glibc, after the stack size has been reduced,
28
       e.g. on x86_64 systems after "ulimit -s 260".
29
       This stack size is only sufficient for ca. 3919 recursions.
30
       Cf. <https://unix.stackexchange.com/questions/620720/>
31
     - On systems with musl libc, because there the thread stack size (for a
32
       thread other than the main thread) by default is only 128 KB, see
33
       <https://wiki.musl-libc.org/functional-differences-from-glibc.html#Thread-stack-size>.
34
     - On AIX 7 systems, because there the thread stack size (for a thread
35
       other than the main thread) by default is only 96 KB, see
36
       <https://www.ibm.com/docs/en/aix/7.1?topic=programming-threads-library-options>.
37
       This stack size is only sufficient for between 887 and 1363 recursions,
38
       depending on the compiler and compiler optimization options.
39
   A maximum depth of 100 is a large enough for all practical needs
40
   and also small enough to avoid stack overflow even with small thread stack
41
   sizes.  */
42
#ifndef EVAL_MAXDEPTH
43
0
# define EVAL_MAXDEPTH 100
44
#endif
45
46
/* A shorthand that denotes a successful evaluation result with a value V.  */
47
0
#define OK(v) (struct eval_result) { .status = PE_OK, .value = (v) }
48
49
/* Evaluates a plural expression PEXP for n=N, up to ALLOWED_DEPTH.  */
50
static struct eval_result
51
plural_eval_recurse (const struct expression *pexp, unsigned long int n,
52
         unsigned int allowed_depth)
53
0
{
54
0
  if (allowed_depth == 0)
55
    /* The allowed recursion depth is exhausted.  */
56
0
    return (struct eval_result) { .status = PE_STACKOVF };
57
0
  allowed_depth--;
58
59
0
  switch (pexp->nargs)
60
0
    {
61
0
    case 0:
62
0
      switch (pexp->operation)
63
0
  {
64
0
  case var:
65
0
    return OK (n);
66
0
  case num:
67
0
    return OK (pexp->val.num);
68
0
  default:
69
0
    break;
70
0
  }
71
      /* NOTREACHED */
72
0
      break;
73
0
    case 1:
74
0
      {
75
  /* pexp->operation must be lnot.  */
76
0
  struct eval_result arg =
77
0
    plural_eval_recurse (pexp->val.args[0], n, allowed_depth);
78
0
  if (arg.status != PE_OK)
79
0
    return arg;
80
0
  return OK (! arg.value);
81
0
      }
82
0
    case 2:
83
0
      {
84
0
  struct eval_result leftarg =
85
0
    plural_eval_recurse (pexp->val.args[0], n, allowed_depth);
86
0
  if (leftarg.status != PE_OK)
87
0
    return leftarg;
88
0
  if (pexp->operation == lor)
89
0
    {
90
0
      if (leftarg.value)
91
0
        return OK (1);
92
0
      struct eval_result rightarg =
93
0
        plural_eval_recurse (pexp->val.args[1], n, allowed_depth);
94
0
      if (rightarg.status != PE_OK)
95
0
        return rightarg;
96
0
      return OK (rightarg.value ? 1 : 0);
97
0
    }
98
0
  else if (pexp->operation == land)
99
0
    {
100
0
      if (!leftarg.value)
101
0
        return OK (0);
102
0
      struct eval_result rightarg =
103
0
        plural_eval_recurse (pexp->val.args[1], n, allowed_depth);
104
0
      if (rightarg.status != PE_OK)
105
0
        return rightarg;
106
0
      return OK (rightarg.value ? 1 : 0);
107
0
    }
108
0
  else
109
0
    {
110
0
      struct eval_result rightarg =
111
0
        plural_eval_recurse (pexp->val.args[1], n, allowed_depth);
112
0
      if (rightarg.status != PE_OK)
113
0
        return rightarg;
114
115
0
      switch (pexp->operation)
116
0
        {
117
0
        case mult:
118
0
    return OK (leftarg.value * rightarg.value);
119
0
        case divide:
120
0
    if (rightarg.value == 0)
121
0
      return (struct eval_result) { .status = PE_INTDIV };
122
0
    return OK (leftarg.value / rightarg.value);
123
0
        case module:
124
0
    if (rightarg.value == 0)
125
0
      return (struct eval_result) { .status = PE_INTDIV };
126
0
    return OK (leftarg.value % rightarg.value);
127
0
        case plus:
128
0
    return OK (leftarg.value + rightarg.value);
129
0
        case minus:
130
0
    return OK (leftarg.value - rightarg.value);
131
0
        case less_than:
132
0
    return OK (leftarg.value < rightarg.value);
133
0
        case greater_than:
134
0
    return OK (leftarg.value > rightarg.value);
135
0
        case less_or_equal:
136
0
    return OK (leftarg.value <= rightarg.value);
137
0
        case greater_or_equal:
138
0
    return OK (leftarg.value >= rightarg.value);
139
0
        case equal:
140
0
    return OK (leftarg.value == rightarg.value);
141
0
        case not_equal:
142
0
    return OK (leftarg.value != rightarg.value);
143
0
        default:
144
0
    break;
145
0
        }
146
0
    }
147
  /* NOTREACHED */
148
0
  break;
149
0
      }
150
0
    case 3:
151
0
      {
152
  /* pexp->operation must be qmop.  */
153
0
  struct eval_result boolarg =
154
0
    plural_eval_recurse (pexp->val.args[0], n, allowed_depth);
155
0
  if (boolarg.status != PE_OK)
156
0
    return boolarg;
157
0
  return plural_eval_recurse (pexp->val.args[boolarg.value ? 1 : 2], n,
158
0
            allowed_depth);
159
0
      }
160
0
    }
161
  /* NOTREACHED */
162
0
  return (struct eval_result) { .status = PE_ASSERT };
163
0
}
164
165
/* Evaluates a plural expression PEXP for n=N.  */
166
STATIC
167
struct eval_result
168
plural_eval (const struct expression *pexp, unsigned long int n)
169
0
{
170
0
  return plural_eval_recurse (pexp, n, EVAL_MAXDEPTH);
171
0
}
172
173
#undef OK