Coverage Report

Created: 2026-07-14 07:01

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
633
{
36
633
  if (args) {
37
633
    if (args->argv && args->allocated) {
38
322
      int i;
39
3.47k
      for (i = 0; i < args->argc; i++)
40
3.14k
        free(args->argv[i]);
41
322
      free(args->argv);
42
322
    }
43
633
    args->argc = 0;
44
633
    args->argv = NULL;
45
633
    args->allocated = 0;
46
633
  }
47
633
}
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.15k
{
57
3.15k
  char **newargv;
58
3.15k
  char *newarg;
59
60
3.15k
  assert(!args->argv || args->allocated);
61
62
3.15k
  newarg = strdup(arg);
63
3.15k
  if (!newarg)
64
0
    return alloc_failed();
65
66
3.15k
  newargv = realloc(args->argv, (args->argc + 2) * sizeof(char *));
67
3.15k
  if (!newargv) {
68
0
    free(newarg);
69
0
    return alloc_failed();
70
0
  }
71
72
3.15k
  args->argv = newargv;
73
3.15k
  args->allocated = 1;
74
3.15k
  args->argv[args->argc++] = newarg;
75
3.15k
  args->argv[args->argc] = NULL;
76
3.15k
  return 0;
77
3.15k
}
78
79
static int fuse_opt_insert_arg_common(struct fuse_args *args, int pos,
80
              const char *arg)
81
340
{
82
340
  assert(pos <= args->argc);
83
340
  if (fuse_opt_add_arg(args, arg) == -1)
84
0
    return -1;
85
86
340
  if (pos != args->argc - 1) {
87
336
    char *newarg = args->argv[args->argc - 1];
88
336
    memmove(&args->argv[pos + 1], &args->argv[pos],
89
336
      sizeof(char *) * (args->argc - pos - 1));
90
336
    args->argv[pos] = newarg;
91
336
  }
92
340
  return 0;
93
340
}
94
95
int fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg)
96
340
{
97
340
  return fuse_opt_insert_arg_common(args, pos, arg);
98
340
}
99
100
static int next_arg(struct fuse_opt_context *ctx, const char *opt)
101
30
{
102
30
  if (ctx->argctr + 1 >= ctx->argc) {
103
11
    fuse_log(FUSE_LOG_ERR, "fuse: missing argument after `%s'\n", opt);
104
11
    return -1;
105
11
  }
106
19
  ctx->argctr++;
107
19
  return 0;
108
30
}
109
110
static int add_arg(struct fuse_opt_context *ctx, const char *arg)
111
2.81k
{
112
2.81k
  return fuse_opt_add_arg(&ctx->outargs, arg);
113
2.81k
}
114
115
static int add_opt_common(char **opts, const char *opt, int esc)
116
2.57k
{
117
2.57k
  unsigned oldlen = *opts ? strlen(*opts) : 0;
118
2.57k
  char *d = realloc(*opts, oldlen + 1 + strlen(opt) * 2 + 1);
119
120
2.57k
  if (!d)
121
0
    return alloc_failed();
122
123
2.57k
  *opts = d;
124
2.57k
  if (oldlen) {
125
1.94k
    d += oldlen;
126
1.94k
    *d++ = ',';
127
1.94k
  }
128
129
14.1k
  for (; *opt; opt++) {
130
11.5k
    if (esc && (*opt == ',' || *opt == '\\'))
131
842
      *d++ = '\\';
132
11.5k
    *d++ = *opt;
133
11.5k
  }
134
2.57k
  *d = '\0';
135
136
2.57k
  return 0;
137
2.57k
}
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
2.57k
{
151
2.57k
  return add_opt_common(&ctx->opts, opt, 1);
152
2.57k
}
153
154
static int call_proc(struct fuse_opt_context *ctx, const char *arg, int key,
155
         int iso)
156
5.05k
{
157
5.05k
  if (key == FUSE_OPT_KEY_DISCARD)
158
0
    return 0;
159
160
5.05k
  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.05k
  if (iso)
166
2.57k
    return add_opt(ctx, arg);
167
2.47k
  else
168
2.47k
    return add_arg(ctx, arg);
169
5.05k
}
170
171
static int match_template(const char *t, const char *arg, unsigned *sepp)
172
12.9k
{
173
12.9k
  const char *sep = strchr(t, '=');
174
12.9k
  sep = sep ? sep : strchr(t, ' ');
175
12.9k
  if (sep && (!sep[1] || sep[1] == '%')) {
176
6.48k
    int arglen = strlen(arg);
177
6.48k
    int tlen = sep - t;
178
6.48k
    if (sep[0] == '=')
179
6.48k
      tlen ++;
180
6.48k
    if (arglen >= tlen && strncmp(arg, t, tlen) == 0) {
181
171
      *sepp = sep - t;
182
171
      return 1;
183
171
    }
184
6.48k
  }
185
12.7k
  if (strcmp(t, arg) == 0) {
186
403
    *sepp = 0;
187
403
    return 1;
188
403
  }
189
12.3k
  return 0;
190
12.7k
}
191
192
static const struct fuse_opt *find_opt(const struct fuse_opt *opt,
193
               const char *arg, unsigned *sepp)
194
3.81k
{
195
16.2k
  for (; opt && opt->templ; opt++)
196
12.9k
    if (match_template(opt->templ, arg, sepp))
197
574
      return opt;
198
3.24k
  return NULL;
199
3.81k
}
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
171
{
210
171
  assert(format[0] == '%');
211
171
  if (format[1] == 's') {
212
171
    char **s = var;
213
171
    char *copy = strdup(param);
214
171
    if (!copy)
215
0
      return alloc_failed();
216
217
171
    free(*s);
218
171
    *s = copy;
219
171
  } 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
171
  return 0;
226
171
}
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
574
{
232
574
  if (opt->offset == -1U) {
233
0
    if (call_proc(ctx, arg, opt->value, iso) == -1)
234
0
      return -1;
235
574
  } else {
236
574
    void *var = (char *)ctx->data + opt->offset;
237
574
    if (sep && opt->templ[sep + 1]) {
238
171
      const char *param = arg + sep;
239
171
      if (opt->templ[sep] == '=')
240
171
        param ++;
241
171
      if (process_opt_param(var, opt->templ + sep + 1,
242
171
                param, arg) == -1)
243
0
        return -1;
244
171
    } else
245
403
      *(int *)var = opt->value;
246
574
  }
247
574
  return 0;
248
574
}
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
258
0
  if (next_arg(ctx, arg) == -1)
259
0
    return -1;
260
261
0
  param = ctx->argv[ctx->argctr];
262
0
  newarg = malloc(sep + strlen(param) + 1);
263
0
  if (!newarg)
264
0
    return alloc_failed();
265
266
0
  memcpy(newarg, arg, sep);
267
0
  strcpy(newarg + sep, param);
268
0
  res = process_opt(ctx, opt, sep, newarg, iso);
269
0
  free(newarg);
270
271
0
  return res;
272
0
}
273
274
static int process_gopt(struct fuse_opt_context *ctx, const char *arg, int iso)
275
3.24k
{
276
3.24k
  unsigned sep;
277
3.24k
  const struct fuse_opt *opt = find_opt(ctx->opt, arg, &sep);
278
3.24k
  if (opt) {
279
1.14k
    for (; opt; opt = find_opt(opt + 1, arg, &sep)) {
280
574
      int res;
281
574
      if (sep && opt->templ[sep] == ' ' && !arg[sep])
282
0
        res = process_opt_sep_arg(ctx, opt, sep, arg,
283
0
                iso);
284
574
      else
285
574
        res = process_opt(ctx, opt, sep, arg, iso);
286
574
      if (res == -1)
287
0
        return -1;
288
574
    }
289
574
    return 0;
290
574
  } else
291
2.66k
    return call_proc(ctx, arg, FUSE_OPT_KEY_OPT, iso);
292
3.24k
}
293
294
static int process_real_option_group(struct fuse_opt_context *ctx, char *opts)
295
326
{
296
326
  char *s = opts;
297
326
  char *d = s;
298
326
  int end = 0;
299
300
17.6k
  while (!end) {
301
17.3k
    if (*s == '\0')
302
326
      end = 1;
303
17.3k
    if (*s == ',' || end) {
304
3.09k
      int res;
305
306
3.09k
      *d = '\0';
307
3.09k
      res = process_gopt(ctx, opts, 1);
308
3.09k
      if (res == -1)
309
0
        return -1;
310
3.09k
      d = opts;
311
14.2k
    } else {
312
14.2k
      if (s[0] == '\\' && s[1] != '\0') {
313
2.81k
        s++;
314
2.81k
        if (s[0] >= '0' && s[0] <= '3' &&
315
1.65k
            s[1] >= '0' && s[1] <= '7' &&
316
967
            s[2] >= '0' && s[2] <= '7') {
317
335
          *d++ = (s[0] - '0') * 0100 +
318
335
            (s[1] - '0') * 0010 +
319
335
            (s[2] - '0');
320
335
          s += 2;
321
2.47k
        } else {
322
2.47k
          *d++ = *s;
323
2.47k
        }
324
11.4k
      } else {
325
11.4k
        *d++ = *s;
326
11.4k
      }
327
14.2k
    }
328
17.3k
    s++;
329
17.3k
  }
330
331
326
  return 0;
332
326
}
333
334
static int process_option_group(struct fuse_opt_context *ctx, const char *opts)
335
326
{
336
326
  int res;
337
326
  char *copy = strdup(opts);
338
339
326
  if (!copy) {
340
0
    fuse_log(FUSE_LOG_ERR, "fuse: memory allocation failed\n");
341
0
    return -1;
342
0
  }
343
326
  res = process_real_option_group(ctx, copy);
344
326
  free(copy);
345
326
  return res;
346
326
}
347
348
static int process_one(struct fuse_opt_context *ctx, const char *arg)
349
2.87k
{
350
2.87k
  if (ctx->nonopt || arg[0] != '-')
351
2.38k
    return call_proc(ctx, arg, FUSE_OPT_KEY_NONOPT, 0);
352
495
  else if (arg[1] == 'o') {
353
337
    if (arg[2])
354
307
      return process_option_group(ctx, arg + 2);
355
30
    else {
356
30
      if (next_arg(ctx, arg) == -1)
357
11
        return -1;
358
359
19
      return process_option_group(ctx,
360
19
                ctx->argv[ctx->argctr]);
361
30
    }
362
337
  } else if (arg[1] == '-' && !arg[2]) {
363
11
    if (add_arg(ctx, arg) == -1)
364
0
      return -1;
365
11
    ctx->nonopt = ctx->outargs.argc;
366
11
    return 0;
367
11
  } else
368
147
    return process_gopt(ctx, arg, 0);
369
2.87k
}
370
371
static int opt_parse(struct fuse_opt_context *ctx)
372
322
{
373
322
  if (ctx->argc) {
374
322
    if (add_arg(ctx, ctx->argv[0]) == -1)
375
0
      return -1;
376
322
  }
377
378
3.19k
  for (ctx->argctr = 1; ctx->argctr < ctx->argc; ctx->argctr++)
379
2.87k
    if (process_one(ctx, ctx->argv[ctx->argctr]) == -1)
380
11
      return -1;
381
382
311
  if (ctx->opts) {
383
170
    if (fuse_opt_insert_arg(&ctx->outargs, 1, "-o") == -1 ||
384
170
        fuse_opt_insert_arg(&ctx->outargs, 2, ctx->opts) == -1)
385
0
      return -1;
386
170
  }
387
388
  /* If option separator ("--") is the last argument, remove it */
389
311
  if (ctx->nonopt && ctx->nonopt == ctx->outargs.argc &&
390
1
      strcmp(ctx->outargs.argv[ctx->outargs.argc - 1], "--") == 0) {
391
1
    free(ctx->outargs.argv[ctx->outargs.argc - 1]);
392
1
    ctx->outargs.argv[--ctx->outargs.argc] = NULL;
393
1
  }
394
395
311
  return 0;
396
311
}
397
398
int fuse_opt_parse(struct fuse_args *args, void *data,
399
       const struct fuse_opt opts[], fuse_opt_proc_t proc)
400
322
{
401
322
  int res;
402
322
  struct fuse_opt_context ctx = {
403
322
    .data = data,
404
322
    .opt = opts,
405
322
    .proc = proc,
406
322
  };
407
408
322
  if (!args || !args->argv || !args->argc)
409
0
    return 0;
410
411
322
  ctx.argc = args->argc;
412
322
  ctx.argv = args->argv;
413
414
322
  res = opt_parse(&ctx);
415
322
  if (res != -1) {
416
311
    struct fuse_args tmp = *args;
417
311
    *args = ctx.outargs;
418
311
    ctx.outargs = tmp;
419
311
  }
420
322
  free(ctx.opts);
421
322
  fuse_opt_free_args(&ctx.outargs);
422
322
  return res;
423
322
}