Coverage Report

Created: 2025-12-14 06:39

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