Coverage Report

Created: 2026-07-30 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libfuse/lib/fuse_opt.c
Line
Count
Source
1
/*
2
  FUSE: Filesystem in Userspace
3
  Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
4
5
  Implementation of option parsing routines (dealing with `struct
6
  fuse_args`).
7
8
  This program can be distributed under the terms of the GNU LGPLv2.
9
  See the file LGPL2.txt
10
*/
11
12
#include "fuse_config.h"
13
#include "fuse_i.h"
14
#include "fuse_opt.h"
15
#include "fuse_misc.h"
16
17
#include <stdio.h>
18
#include <stdlib.h>
19
#include <string.h>
20
#include <assert.h>
21
22
struct fuse_opt_context {
23
  void *data;
24
  const struct fuse_opt *opt;
25
  fuse_opt_proc_t proc;
26
  int argctr;
27
  int argc;
28
  char **argv;
29
  struct fuse_args outargs;
30
  char *opts;
31
  int nonopt;
32
};
33
34
void fuse_opt_free_args(struct fuse_args *args)
35
646
{
36
646
  if (args) {
37
646
    if (args->argv && args->allocated) {
38
328
      int i;
39
3.53k
      for (i = 0; i < args->argc; i++)
40
3.21k
        free(args->argv[i]);
41
328
      free(args->argv);
42
328
    }
43
646
    args->argc = 0;
44
646
    args->argv = NULL;
45
646
    args->allocated = 0;
46
646
  }
47
646
}
48
49
static int alloc_failed(void)
50
0
{
51
0
  fuse_log(FUSE_LOG_ERR, "fuse: memory allocation failed\n");
52
0
  return -1;
53
0
}
54
55
int fuse_opt_add_arg(struct fuse_args *args, const char *arg)
56
3.21k
{
57
3.21k
  char **newargv;
58
3.21k
  char *newarg;
59
60
3.21k
  assert(!args->argv || args->allocated);
61
62
3.21k
  newarg = strdup(arg);
63
3.21k
  if (!newarg)
64
0
    return alloc_failed();
65
66
3.21k
  newargv = realloc(args->argv, (args->argc + 2) * sizeof(char *));
67
3.21k
  if (!newargv) {
68
0
    free(newarg);
69
0
    return alloc_failed();
70
0
  }
71
72
3.21k
  args->argv = newargv;
73
3.21k
  args->allocated = 1;
74
3.21k
  args->argv[args->argc++] = newarg;
75
3.21k
  args->argv[args->argc] = NULL;
76
3.21k
  return 0;
77
3.21k
}
78
79
static int fuse_opt_insert_arg_common(struct fuse_args *args, int pos,
80
              const char *arg)
81
362
{
82
362
  assert(pos <= args->argc);
83
362
  if (fuse_opt_add_arg(args, arg) == -1)
84
0
    return -1;
85
86
362
  if (pos != args->argc - 1) {
87
358
    char *newarg = args->argv[args->argc - 1];
88
358
    memmove(&args->argv[pos + 1], &args->argv[pos],
89
358
      sizeof(char *) * (args->argc - pos - 1));
90
358
    args->argv[pos] = newarg;
91
358
  }
92
362
  return 0;
93
362
}
94
95
int fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg)
96
362
{
97
362
  return fuse_opt_insert_arg_common(args, pos, arg);
98
362
}
99
100
static int next_arg(struct fuse_opt_context *ctx, const char *opt)
101
37
{
102
37
  if (ctx->argctr + 1 >= ctx->argc) {
103
10
    fuse_log(FUSE_LOG_ERR, "fuse: missing argument after `%s'\n", opt);
104
10
    return -1;
105
10
  }
106
27
  ctx->argctr++;
107
27
  return 0;
108
37
}
109
110
static int add_arg(struct fuse_opt_context *ctx, const char *arg)
111
2.85k
{
112
2.85k
  return fuse_opt_add_arg(&ctx->outargs, arg);
113
2.85k
}
114
115
static int add_opt_common(char **opts, const char *opt, int esc)
116
3.19k
{
117
3.19k
  unsigned oldlen = *opts ? strlen(*opts) : 0;
118
3.19k
  char *d = realloc(*opts, oldlen + 1 + strlen(opt) * 2 + 1);
119
120
3.19k
  if (!d)
121
0
    return alloc_failed();
122
123
3.19k
  *opts = d;
124
3.19k
  if (oldlen) {
125
2.61k
    d += oldlen;
126
2.61k
    *d++ = ',';
127
2.61k
  }
128
129
14.9k
  for (; *opt; opt++) {
130
11.7k
    if (esc && (*opt == ',' || *opt == '\\'))
131
1.03k
      *d++ = '\\';
132
11.7k
    *d++ = *opt;
133
11.7k
  }
134
3.19k
  *d = '\0';
135
136
3.19k
  return 0;
137
3.19k
}
138
139
int fuse_opt_add_opt(char **opts, const char *opt)
140
0
{
141
0
  return add_opt_common(opts, opt, 0);
142
0
}
143
144
int fuse_opt_add_opt_escaped(char **opts, const char *opt)
145
0
{
146
0
  return add_opt_common(opts, opt, 1);
147
0
}
148
149
static int add_opt(struct fuse_opt_context *ctx, const char *opt)
150
3.19k
{
151
3.19k
  return add_opt_common(&ctx->opts, opt, 1);
152
3.19k
}
153
154
static int call_proc(struct fuse_opt_context *ctx, const char *arg, int key,
155
         int iso)
156
5.70k
{
157
5.70k
  if (key == FUSE_OPT_KEY_DISCARD)
158
0
    return 0;
159
160
5.70k
  if (key != FUSE_OPT_KEY_KEEP && ctx->proc) {
161
0
    int res = ctx->proc(ctx->data, arg, key, &ctx->outargs);
162
0
    if (res == -1 || !res)
163
0
      return res;
164
0
  }
165
5.70k
  if (iso)
166
3.19k
    return add_opt(ctx, arg);
167
2.51k
  else
168
2.51k
    return add_arg(ctx, arg);
169
5.70k
}
170
171
static int match_template(const char *t, const char *arg, unsigned *sepp)
172
15.1k
{
173
15.1k
  const char *sep = strchr(t, '=');
174
15.1k
  sep = sep ? sep : strchr(t, ' ');
175
15.1k
  if (sep && (!sep[1] || sep[1] == '%')) {
176
7.59k
    int arglen = strlen(arg);
177
7.59k
    int tlen = sep - t;
178
7.59k
    if (sep[0] == '=')
179
7.59k
      tlen ++;
180
7.59k
    if (arglen >= tlen && strncmp(arg, t, tlen) == 0) {
181
227
      *sepp = sep - t;
182
227
      return 1;
183
227
    }
184
7.59k
  }
185
14.9k
  if (strcmp(t, arg) == 0) {
186
279
    *sepp = 0;
187
279
    return 1;
188
279
  }
189
14.6k
  return 0;
190
14.9k
}
191
192
static const struct fuse_opt *find_opt(const struct fuse_opt *opt,
193
               const char *arg, unsigned *sepp)
194
4.30k
{
195
18.9k
  for (; opt && opt->templ; opt++)
196
15.1k
    if (match_template(opt->templ, arg, sepp))
197
506
      return opt;
198
3.79k
  return NULL;
199
4.30k
}
200
201
int fuse_opt_match(const struct fuse_opt *opts, const char *opt)
202
0
{
203
0
  unsigned dummy;
204
0
  return find_opt(opts, opt, &dummy) ? 1 : 0;
205
0
}
206
207
static int process_opt_param(void *var, const char *format, const char *param,
208
           const char *arg)
209
227
{
210
227
  assert(format[0] == '%');
211
227
  if (format[1] == 's') {
212
227
    char **s = var;
213
227
    char *copy = strdup(param);
214
227
    if (!copy)
215
0
      return alloc_failed();
216
217
227
    free(*s);
218
227
    *s = copy;
219
227
  } else {
220
0
    if (sscanf(param, format, var) != 1) {
221
0
      fuse_log(FUSE_LOG_ERR, "fuse: invalid parameter in option `%s'\n", arg);
222
0
      return -1;
223
0
    }
224
0
  }
225
227
  return 0;
226
227
}
227
228
static int process_opt(struct fuse_opt_context *ctx,
229
           const struct fuse_opt *opt, unsigned sep,
230
           const char *arg, int iso)
231
506
{
232
506
  if (opt->offset == -1U) {
233
0
    if (call_proc(ctx, arg, opt->value, iso) == -1)
234
0
      return -1;
235
506
  } else {
236
506
    void *var = (char *)ctx->data + opt->offset;
237
506
    if (sep && opt->templ[sep + 1]) {
238
227
      const char *param = arg + sep;
239
227
      if (opt->templ[sep] == '=')
240
227
        param ++;
241
227
      if (process_opt_param(var, opt->templ + sep + 1,
242
227
                param, arg) == -1)
243
0
        return -1;
244
227
    } else
245
279
      *(int *)var = opt->value;
246
506
  }
247
506
  return 0;
248
506
}
249
250
static int process_opt_sep_arg(struct fuse_opt_context *ctx,
251
             const struct fuse_opt *opt, unsigned sep,
252
             const char *arg, int iso)
253
0
{
254
0
  int res;
255
0
  char *newarg;
256
0
  const char *param;
257
0
  size_t newarglen;
258
259
0
  if (next_arg(ctx, arg) == -1)
260
0
    return -1;
261
262
0
  param = ctx->argv[ctx->argctr];
263
0
  newarglen = sep + strlen(param) + 1;
264
0
  newarg = malloc(newarglen);
265
0
  if (!newarg)
266
0
    return alloc_failed();
267
268
0
  memcpy(newarg, arg, sep);
269
0
  snprintf(newarg + sep, newarglen - sep, "%s", param);
270
0
  res = process_opt(ctx, opt, sep, newarg, iso);
271
0
  free(newarg);
272
273
0
  return res;
274
0
}
275
276
static int process_gopt(struct fuse_opt_context *ctx, const char *arg, int iso)
277
3.79k
{
278
3.79k
  unsigned sep;
279
3.79k
  const struct fuse_opt *opt = find_opt(ctx->opt, arg, &sep);
280
3.79k
  if (opt) {
281
1.01k
    for (; opt; opt = find_opt(opt + 1, arg, &sep)) {
282
506
      int res;
283
506
      if (sep && opt->templ[sep] == ' ' && !arg[sep])
284
0
        res = process_opt_sep_arg(ctx, opt, sep, arg,
285
0
                iso);
286
506
      else
287
506
        res = process_opt(ctx, opt, sep, arg, iso);
288
506
      if (res == -1)
289
0
        return -1;
290
506
    }
291
506
    return 0;
292
506
  } else
293
3.29k
    return call_proc(ctx, arg, FUSE_OPT_KEY_OPT, iso);
294
3.79k
}
295
296
static int process_real_option_group(struct fuse_opt_context *ctx, char *opts)
297
354
{
298
354
  char *s = opts;
299
354
  char *d = s;
300
354
  int end = 0;
301
302
19.2k
  while (!end) {
303
18.9k
    if (*s == '\0')
304
354
      end = 1;
305
18.9k
    if (*s == ',' || end) {
306
3.66k
      int res;
307
308
3.66k
      *d = '\0';
309
3.66k
      res = process_gopt(ctx, opts, 1);
310
3.66k
      if (res == -1)
311
0
        return -1;
312
3.66k
      d = opts;
313
15.2k
    } else {
314
15.2k
      if (s[0] == '\\' && s[1] != '\0') {
315
2.86k
        s++;
316
2.86k
        if (s[0] >= '0' && s[0] <= '3' &&
317
1.50k
            s[1] >= '0' && s[1] <= '7' &&
318
870
            s[2] >= '0' && s[2] <= '7') {
319
229
          *d++ = (s[0] - '0') * 0100 +
320
229
            (s[1] - '0') * 0010 +
321
229
            (s[2] - '0');
322
229
          s += 2;
323
2.63k
        } else {
324
2.63k
          *d++ = *s;
325
2.63k
        }
326
12.3k
      } else {
327
12.3k
        *d++ = *s;
328
12.3k
      }
329
15.2k
    }
330
18.9k
    s++;
331
18.9k
  }
332
333
354
  return 0;
334
354
}
335
336
static int process_option_group(struct fuse_opt_context *ctx, const char *opts)
337
354
{
338
354
  int res;
339
354
  char *copy = strdup(opts);
340
341
354
  if (!copy) {
342
0
    fuse_log(FUSE_LOG_ERR, "fuse: memory allocation failed\n");
343
0
    return -1;
344
0
  }
345
354
  res = process_real_option_group(ctx, copy);
346
354
  free(copy);
347
354
  return res;
348
354
}
349
350
static int process_one(struct fuse_opt_context *ctx, const char *arg)
351
2.92k
{
352
2.92k
  if (ctx->nonopt || arg[0] != '-')
353
2.41k
    return call_proc(ctx, arg, FUSE_OPT_KEY_NONOPT, 0);
354
507
  else if (arg[1] == 'o') {
355
364
    if (arg[2])
356
327
      return process_option_group(ctx, arg + 2);
357
37
    else {
358
37
      if (next_arg(ctx, arg) == -1)
359
10
        return -1;
360
361
27
      return process_option_group(ctx,
362
27
                ctx->argv[ctx->argctr]);
363
37
    }
364
364
  } else if (arg[1] == '-' && !arg[2]) {
365
10
    if (add_arg(ctx, arg) == -1)
366
0
      return -1;
367
10
    ctx->nonopt = ctx->outargs.argc;
368
10
    return 0;
369
10
  } else
370
133
    return process_gopt(ctx, arg, 0);
371
2.92k
}
372
373
static int opt_parse(struct fuse_opt_context *ctx)
374
328
{
375
328
  if (ctx->argc) {
376
328
    if (add_arg(ctx, ctx->argv[0]) == -1)
377
0
      return -1;
378
328
  }
379
380
3.24k
  for (ctx->argctr = 1; ctx->argctr < ctx->argc; ctx->argctr++)
381
2.92k
    if (process_one(ctx, ctx->argv[ctx->argctr]) == -1)
382
10
      return -1;
383
384
318
  if (ctx->opts) {
385
181
    if (fuse_opt_insert_arg(&ctx->outargs, 1, "-o") == -1 ||
386
181
        fuse_opt_insert_arg(&ctx->outargs, 2, ctx->opts) == -1)
387
0
      return -1;
388
181
  }
389
390
  /* If option separator ("--") is the last argument, remove it */
391
318
  if (ctx->nonopt && ctx->nonopt == ctx->outargs.argc &&
392
1
      strcmp(ctx->outargs.argv[ctx->outargs.argc - 1], "--") == 0) {
393
1
    free(ctx->outargs.argv[ctx->outargs.argc - 1]);
394
1
    ctx->outargs.argv[--ctx->outargs.argc] = NULL;
395
1
  }
396
397
318
  return 0;
398
318
}
399
400
int fuse_opt_parse(struct fuse_args *args, void *data,
401
       const struct fuse_opt opts[], fuse_opt_proc_t proc)
402
328
{
403
328
  int res;
404
328
  struct fuse_opt_context ctx = {
405
328
    .data = data,
406
328
    .opt = opts,
407
328
    .proc = proc,
408
328
  };
409
410
328
  if (!args || !args->argv || !args->argc)
411
0
    return 0;
412
413
328
  ctx.argc = args->argc;
414
328
  ctx.argv = args->argv;
415
416
328
  res = opt_parse(&ctx);
417
328
  if (res != -1) {
418
318
    struct fuse_args tmp = *args;
419
318
    *args = ctx.outargs;
420
318
    ctx.outargs = tmp;
421
318
  }
422
328
  free(ctx.opts);
423
328
  fuse_opt_free_args(&ctx.outargs);
424
328
  return res;
425
328
}