Bug Summary

File:builds/wireshark/wireshark/wsutil/filter_files.c
Warning:line 164, column 21
File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name filter_files.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -fno-delete-null-pointer-checks -mframe-pointer=all -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -ffloat16-excess-precision=fast -fbfloat16-excess-precision=fast -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/builds/wireshark/wireshark/build -fcoverage-compilation-dir=/builds/wireshark/wireshark/build -resource-dir /usr/lib/llvm-22/lib/clang/22 -isystem /usr/include/glib-2.0 -isystem /usr/lib/x86_64-linux-gnu/glib-2.0/include -D BUILD_WSUTIL -D CARES_NO_DEPRECATED -D G_DISABLE_DEPRECATED -D G_DISABLE_SINGLE_INCLUDES -D WS_BUILD_DLL -D WS_DEBUG -D WS_DEBUG_UTF_8 -D wsutil_EXPORTS -I /builds/wireshark/wireshark/build -I /builds/wireshark/wireshark -I /builds/wireshark/wireshark/include -I /builds/wireshark/wireshark/build/wsutil -D _GLIBCXX_ASSERTIONS -internal-isystem /usr/lib/llvm-22/lib/clang/22/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/builds/wireshark/wireshark/= -fmacro-prefix-map=/builds/wireshark/wireshark/build/= -fmacro-prefix-map=../= -Wno-format-nonliteral -std=gnu17 -ferror-limit 19 -fvisibility=hidden -fwrapv -fwrapv-pointer -fstrict-flex-arrays=3 -stack-protector 2 -fstack-clash-protection -fcf-protection=full -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fexceptions -fcolor-diagnostics -analyzer-output=html -faddrsig -fdwarf2-cfi-asm -o /builds/wireshark/wireshark/sbout/2026-11-25-100324-3661-1 -x c /builds/wireshark/wireshark/wsutil/filter_files.c
1/* filter_files.c
2 * Code for reading and writing the filters file.
3 *
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11#include "config.h"
12#define WS_LOG_DOMAIN"WSUtil" LOG_DOMAIN_WSUTIL"WSUtil"
13#include "filter_files.h"
14
15#include <stdio.h>
16#include <string.h>
17#include <errno(*__errno_location ()).h>
18
19#include <wsutil/file_util.h>
20#include <wsutil/filesystem.h>
21#include <wsutil/profile_inherit.h>
22#include <wsutil/report_message.h>
23#include <wsutil/wslog.h>
24#include <wsutil/ws_assert.h>
25
26/*
27 * Read in a list of filters.
28 *
29 * On error, report the error via the UI.
30 */
31
32#define INIT_BUF_SIZE128 128
33
34static GList *
35add_filter_entry(GList *fl, const char *filt_name, const char *filt_expr)
36{
37 filter_def *filt;
38
39 filt = g_new(filter_def, 1)((filter_def *) g_malloc_n ((1), sizeof (filter_def)));
40 filt->name = g_strdup(filt_name)g_strdup_inline (filt_name);
41 filt->strval = g_strdup(filt_expr)g_strdup_inline (filt_expr);
42 filt->source_profile = NULL((void*)0);
43 return g_list_prepend(fl, filt);
44}
45
46static void
47free_filter_entry(void * data)
48{
49 filter_def *filt = (filter_def*)data;
50 g_free(filt->name)(__builtin_object_size ((filt->name), 0) != ((size_t) - 1)
) ? g_free_sized (filt->name, __builtin_object_size ((filt
->name), 0)) : (g_free) (filt->name)
;
51 g_free(filt->strval)(__builtin_object_size ((filt->strval), 0) != ((size_t) - 1
)) ? g_free_sized (filt->strval, __builtin_object_size ((filt
->strval), 0)) : (g_free) (filt->strval)
;
52 g_free(filt->source_profile)(__builtin_object_size ((filt->source_profile), 0) != ((size_t
) - 1)) ? g_free_sized (filt->source_profile, __builtin_object_size
((filt->source_profile), 0)) : (g_free) (filt->source_profile
)
;
53 g_free(filt)(__builtin_object_size ((filt), 0) != ((size_t) - 1)) ? g_free_sized
(filt, __builtin_object_size ((filt), 0)) : (g_free) (filt)
;
54}
55
56void ws_filter_list_free(filter_list_t *fl)
57{
58 g_list_free_full(fl->list, free_filter_entry);
59 g_free(fl)(__builtin_object_size ((fl), 0) != ((size_t) - 1)) ? g_free_sized
(fl, __builtin_object_size ((fl), 0)) : (g_free) (fl)
;
60}
61
62static GList *
63remove_filter_entry(GList *fl, GList *fl_entry)
64{
65 filter_def *filt;
66
67 filt = (filter_def *) fl_entry->data;
68 g_free(filt->name)(__builtin_object_size ((filt->name), 0) != ((size_t) - 1)
) ? g_free_sized (filt->name, __builtin_object_size ((filt
->name), 0)) : (g_free) (filt->name)
;
69 g_free(filt->strval)(__builtin_object_size ((filt->strval), 0) != ((size_t) - 1
)) ? g_free_sized (filt->strval, __builtin_object_size ((filt
->strval), 0)) : (g_free) (filt->strval)
;
70 g_free(filt->source_profile)(__builtin_object_size ((filt->source_profile), 0) != ((size_t
) - 1)) ? g_free_sized (filt->source_profile, __builtin_object_size
((filt->source_profile), 0)) : (g_free) (filt->source_profile
)
;
71 g_free(filt)(__builtin_object_size ((filt), 0) != ((size_t) - 1)) ? g_free_sized
(filt, __builtin_object_size ((filt), 0)) : (g_free) (filt)
;
72 return g_list_remove_link(fl, fl_entry);
73}
74
75static int
76skip_whitespace(FILE *ff)
77{
78 int c;
79
80 while ((c = getc(ff)) != EOF(-1) && c != '\n' && g_ascii_isspace(c)((g_ascii_table[(guchar) (c)] & G_ASCII_SPACE) != 0))
81 ;
82 return c;
83}
84
85static int
86getc_crlf(FILE *ff)
87{
88 int c;
89
90 c = getc(ff);
91 if (c == '\r') {
92 /* Treat CR-LF at the end of a line like LF, so that if we're reading
93 * a Windows-format file on UN*X, we handle it the same way we'd handle
94 * a UN*X-format file. */
95 c = getc(ff);
96 if (c != EOF(-1) && c != '\n') {
97 /* Put back the character after the CR, and process the CR normally. */
98 ungetc(c, ff);
99 c = '\r';
100 }
101 }
102 return c;
103}
104
105static const char *
106filter_list_file_name(filter_list_type_t list_type, const char **description)
107{
108 switch (list_type) {
109
110 case CFILTER_LIST:
111 *description = "capture filter";
112 return CFILTER_FILE_NAME"cfilters";
113
114 case DFILTER_LIST:
115 *description = "display filter";
116 return DFILTER_FILE_NAME"dfilters";
117
118 case DMACROS_LIST:
119 *description = "display filter macro";
120 return DMACROS_FILE_NAME"dmacros";
121 }
122
123 ws_assert_not_reached()ws_log_fatal_full("WSUtil", LOG_LEVEL_ERROR, "wsutil/filter_files.c"
, 123, __func__, "assertion \"not reached\" failed")
;
124}
125
126/*
127 * Read the filters of an open file, prepending them to flp.
128 */
129static GList *
130read_filter_file(FILE *ff, const char *ff_path, const char *ff_description, GList *flp)
131{
132 int c;
133 char *filt_name, *filt_expr;
134 size_t filt_name_len, filt_expr_len;
135 size_t filt_name_index, filt_expr_index;
136 int line = 1;
137
138 /* Allocate the filter name buffer. */
139 filt_name_len = INIT_BUF_SIZE128;
140 filt_name = (char *)g_malloc(filt_name_len + 1);
141 filt_expr_len = INIT_BUF_SIZE128;
142 filt_expr = (char *)g_malloc(filt_expr_len + 1);
143
144 for (line = 1; ; line++) {
5
Loop condition is true. Entering loop body
145 /* Lines in a filter file are of the form
146
147 "name" expression
148
149 where "name" is a name, in quotes - backslashes in the name
150 escape the next character, so quotes and backslashes can appear
151 in the name - and "expression" is a filter expression, not in
152 quotes, running to the end of the line. */
153
154 /* Skip over leading white space, if any. */
155 c = skip_whitespace(ff);
156
157 if (c == EOF(-1))
6
Taking false branch
158 break; /* Nothing more to read */
159 if (c == '\n')
7
Taking false branch
160 continue; /* Blank line. */
161 if (c == '#') {
8
Assuming the condition is true
9
Taking true branch
162 /* Comment. */
163 while (c != '\n')
10
Loop condition is true. Entering loop body
12
Loop condition is true. Entering loop body
164 c = getc(ff); /* skip to the end of the line */
11
Assuming this stream operation fails
13
File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior
165 continue;
166 }
167
168 /* "c" is the first non-white-space character.
169 If it's not a quote, it's an error. */
170 if (c != '"') {
171 ws_warning("'%s' line %d doesn't have a quoted filter name.", ff_path,do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c"
, 172, __func__, "'%s' line %d doesn't have a quoted filter name."
, ff_path, line); } } while (0)
172 line)do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c"
, 172, __func__, "'%s' line %d doesn't have a quoted filter name."
, ff_path, line); } } while (0)
;
173 while (c != '\n')
174 c = getc(ff); /* skip to the end of the line */
175 continue;
176 }
177
178 /* Get the name of the filter. */
179 filt_name_index = 0;
180 for (;;) {
181 c = getc_crlf(ff);
182 if (c == EOF(-1) || c == '\n')
183 break; /* End of line - or end of file */
184 if (c == '"') {
185 /* Closing quote. */
186 if (filt_name_index >= filt_name_len) {
187 /* Filter name buffer isn't long enough; double its length. */
188 filt_name_len *= 2;
189 filt_name = (char *)g_realloc(filt_name, filt_name_len + 1);
190 }
191 filt_name[filt_name_index] = '\0';
192 break;
193 }
194 if (c == '\\') {
195 /* Next character is escaped */
196 c = getc_crlf(ff);
197 if (c == EOF(-1) || c == '\n')
198 break; /* End of line - or end of file */
199 }
200 /* Add this character to the filter name string. */
201 if (filt_name_index >= filt_name_len) {
202 /* Filter name buffer isn't long enough; double its length. */
203 filt_name_len *= 2;
204 filt_name = (char *)g_realloc(filt_name, filt_name_len + 1);
205 }
206 filt_name[filt_name_index] = c;
207 filt_name_index++;
208 }
209
210 if (c == EOF(-1)) {
211 if (!ferror(ff)) {
212 /* EOF, not error; no newline seen before EOF */
213 ws_warning("'%s' line %d doesn't have a newline.", ff_path,do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c"
, 214, __func__, "'%s' line %d doesn't have a newline.", ff_path
, line); } } while (0)
214 line)do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c"
, 214, __func__, "'%s' line %d doesn't have a newline.", ff_path
, line); } } while (0)
;
215 }
216 break; /* nothing more to read */
217 }
218
219 if (c != '"') {
220 /* No newline seen before end-of-line */
221 ws_warning("'%s' line %d doesn't have a closing quote.", ff_path,do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c"
, 222, __func__, "'%s' line %d doesn't have a closing quote."
, ff_path, line); } } while (0)
222 line)do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c"
, 222, __func__, "'%s' line %d doesn't have a closing quote."
, ff_path, line); } } while (0)
;
223 continue;
224 }
225
226 /* Skip over separating white space, if any. */
227 c = skip_whitespace(ff);
228
229 if (c == EOF(-1)) {
230 if (!ferror(ff)) {
231 /* EOF, not error; no newline seen before EOF */
232 ws_warning("'%s' line %d doesn't have a newline.", ff_path,do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c"
, 233, __func__, "'%s' line %d doesn't have a newline.", ff_path
, line); } } while (0)
233 line)do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c"
, 233, __func__, "'%s' line %d doesn't have a newline.", ff_path
, line); } } while (0)
;
234 }
235 break; /* nothing more to read */
236 }
237
238 if (c == '\n') {
239 /* No filter expression */
240 ws_warning("'%s' line %d doesn't have a filter expression.", ff_path,do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c"
, 241, __func__, "'%s' line %d doesn't have a filter expression."
, ff_path, line); } } while (0)
241 line)do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c"
, 241, __func__, "'%s' line %d doesn't have a filter expression."
, ff_path, line); } } while (0)
;
242 continue;
243 }
244
245 /* "c" is the first non-white-space character; it's the first
246 character of the filter expression. */
247 filt_expr_index = 0;
248 for (;;) {
249 /* Add this character to the filter expression string. */
250 if (filt_expr_index >= filt_expr_len) {
251 /* Filter expression buffer isn't long enough; double its length. */
252 filt_expr_len *= 2;
253 filt_expr = (char *)g_realloc(filt_expr, filt_expr_len + 1);
254 }
255 filt_expr[filt_expr_index] = c;
256 filt_expr_index++;
257
258 /* Get the next character. */
259 c = getc_crlf(ff);
260 if (c == EOF(-1) || c == '\n')
261 break;
262 }
263
264 if (c == EOF(-1)) {
265 if (!ferror(ff)) {
266 /* EOF, not error; no newline seen before EOF */
267 ws_warning("'%s' line %d doesn't have a newline.", ff_path,do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c"
, 268, __func__, "'%s' line %d doesn't have a newline.", ff_path
, line); } } while (0)
268 line)do { if (1) { ws_log_full("WSUtil", LOG_LEVEL_WARNING, "wsutil/filter_files.c"
, 268, __func__, "'%s' line %d doesn't have a newline.", ff_path
, line); } } while (0)
;
269 }
270 break; /* nothing more to read */
271 }
272
273 /* We saw the ending newline; terminate the filter expression string */
274 if (filt_expr_index >= filt_expr_len) {
275 /* Filter expression buffer isn't long enough; double its length. */
276 filt_expr_len *= 2;
277 filt_expr = (char *)g_realloc(filt_expr, filt_expr_len + 1);
278 }
279 filt_expr[filt_expr_index] = '\0';
280
281 /* Add the new filter to the list of filters */
282 flp = add_filter_entry(flp, filt_name, filt_expr);
283 }
284 if (ferror(ff)) {
285 report_warning("Error reading your %s file\n\"%s\": %s.",
286 ff_description, ff_path, g_strerror(errno(*__errno_location ())));
287 }
288 g_free(filt_name)(__builtin_object_size ((filt_name), 0) != ((size_t) - 1)) ? g_free_sized
(filt_name, __builtin_object_size ((filt_name), 0)) : (g_free
) (filt_name)
;
289 g_free(filt_expr)(__builtin_object_size ((filt_expr), 0) != ((size_t) - 1)) ? g_free_sized
(filt_expr, __builtin_object_size ((filt_expr), 0)) : (g_free
) (filt_expr)
;
290 return flp;
291}
292
293/*
294 * Read the profile's own filters. A profile using the parent's filters only
295 * has none, and only a profile using its own filters only falls back to the
296 * global file.
297 */
298static filter_list_t *
299filter_list_read_own(filter_list_type_t list_type, const char* app_env_var_prefix, profile_inherit_e mode)
300{
301 const char *ff_name, *ff_description;
302 char *ff_path;
303 FILE *ff;
304
305 filter_list_t *list = g_new(filter_list_t, 1)((filter_list_t *) g_malloc_n ((1), sizeof (filter_list_t)));
306 list->type = list_type;
307 list->list = NULL((void*)0);
308
309 ff_name = filter_list_file_name(list_type, &ff_description);
310
311 if (mode
1.1
'mode' is not equal to PROFILE_INHERIT_PARENT_ONLY
== PROFILE_INHERIT_PARENT_ONLY)
2
Taking false branch
312 return list;
313
314 /* try to open personal "cfilters"/"dfilters" file */
315 ff_path = get_persconffile_path(ff_name, true1, app_env_var_prefix);
316 if ((ff = ws_fopenfopen(ff_path, "r")) == NULL((void*)0)) {
3
Taking false branch
317 /*
318 * Did that fail because the file didn't exist?
319 */
320 if (errno(*__errno_location ()) != ENOENT2) {
321 /*
322 * No. Just give up.
323 */
324 report_warning("Could not open your %s file\n\"%s\": %s.",
325 ff_description, ff_path, g_strerror(errno(*__errno_location ())));
326 g_free(ff_path)(__builtin_object_size ((ff_path), 0) != ((size_t) - 1)) ? g_free_sized
(ff_path, __builtin_object_size ((ff_path), 0)) : (g_free) (
ff_path)
;
327 return list;
328 }
329
330 g_free(ff_path)(__builtin_object_size ((ff_path), 0) != ((size_t) - 1)) ? g_free_sized
(ff_path, __builtin_object_size ((ff_path), 0)) : (g_free) (
ff_path)
;
331 if (mode != PROFILE_INHERIT_PROFILE_ONLY) {
332 /* The global filters come from the parent profiles */
333 return list;
334 }
335
336 /*
337 * Yes. Try to open the global "cfilters/dfilters" file.
338 */
339 ff_path = get_datafile_path(ff_name, app_env_var_prefix);
340 if ((ff = ws_fopenfopen(ff_path, "r")) == NULL((void*)0)) {
341 /*
342 * Well, that didn't work, either. Just give up.
343 * Report an error if the file existed but we couldn't open it.
344 */
345 if (errno(*__errno_location ()) != ENOENT2) {
346 report_warning("Could not open your %s file\n\"%s\": %s.",
347 ff_description, ff_path, g_strerror(errno(*__errno_location ())));
348 }
349 g_free(ff_path)(__builtin_object_size ((ff_path), 0) != ((size_t) - 1)) ? g_free_sized
(ff_path, __builtin_object_size ((ff_path), 0)) : (g_free) (
ff_path)
;
350 return list;
351 }
352 }
353
354 list->list = read_filter_file(ff, ff_path, ff_description, NULL((void*)0));
4
Calling 'read_filter_file'
355 g_free(ff_path)(__builtin_object_size ((ff_path), 0) != ((size_t) - 1)) ? g_free_sized
(ff_path, __builtin_object_size ((ff_path), 0)) : (g_free) (
ff_path)
;
356 fclose(ff);
357 return list;
358}
359
360typedef struct {
361 filter_list_type_t type;
362 const char *description;
363 GList *entries; /* The inherited filters, in reverse order */
364} read_layer_data_t;
365
366/* Read the filters of one parent profile */
367static bool_Bool
368read_layer_filters(const profile_inherit_layer_t *layer, const char *path, const char *filename _U___attribute__((unused)),
369 profile_inherit_seen_t *seen, void *user_data, char **err)
370{
371 read_layer_data_t *data = (read_layer_data_t *)user_data;
372 FILE *ff;
373
374 if ((ff = ws_fopenfopen(path, "r")) == NULL((void*)0)) {
375 *err = ws_strdup_printf("Could not open the %s file \"%s\": %s.",wmem_strdup_printf(((void*)0), "Could not open the %s file \"%s\": %s."
, data->description, path, g_strerror((*__errno_location (
))))
376 data->description, path, g_strerror(errno))wmem_strdup_printf(((void*)0), "Could not open the %s file \"%s\": %s."
, data->description, path, g_strerror((*__errno_location (
))))
;
377 return false0;
378 }
379 /* In the order of the file */
380 GList *layer_list = g_list_reverse(read_filter_file(ff, path, data->description, NULL((void*)0)));
381 fclose(ff);
382
383 for (GList *item = layer_list; item; item = g_list_next(item)((item) ? (((GList *)(item))->next) : ((void*)0))) {
384 filter_def *filt = (filter_def *)item->data;
385
386 if (profile_inherit_seen_check(seen, filt->name)) {
387 free_filter_entry(filt);
388 continue;
389 }
390 filt->source_profile = g_strdup(layer->profile_name)g_strdup_inline (layer->profile_name);
391 data->entries = g_list_prepend(data->entries, filt);
392 }
393 g_list_free(layer_list);
394 return true1;
395}
396
397filter_list_t *
398ws_filter_list_read(filter_list_type_t list_type, const char* app_env_var_prefix)
399{
400 const char *ff_name, *ff_description;
401
402 ff_name = filter_list_file_name(list_type, &ff_description);
403 profile_inherit_e mode = profile_get_inherit(app_env_var_prefix, NULL((void*)0), ff_name);
404
405 filter_list_t *list = filter_list_read_own(list_type, app_env_var_prefix, mode);
406 if (mode == PROFILE_INHERIT_PROFILE_ONLY)
407 return list;
408
409 /* The profile's own filters come first, and win over duplicates from parents */
410 profile_inherit_seen_t *seen = profile_inherit_seen_new();
411 for (GList *item = list->list; item; item = g_list_next(item)((item) ? (((GList *)(item))->next) : ((void*)0))) {
412 const filter_def *filt = (const filter_def *)item->data;
413 profile_inherit_seen_check(seen, filt->name);
414 }
415 profile_inherit_seen_next(seen);
416
417 read_layer_data_t data = { list_type, ff_description, NULL((void*)0) };
418 char *err = NULL((void*)0);
419 if (!profile_inherit_read_layers(app_env_var_prefix, ff_name, NULL((void*)0), true1, mode, seen,
420 read_layer_filters, &data, &err)) {
421 report_warning("%s", err);
422 }
423 g_free(err)(__builtin_object_size ((err), 0) != ((size_t) - 1)) ? g_free_sized
(err, __builtin_object_size ((err), 0)) : (g_free) (err)
;
424 profile_inherit_seen_free(seen);
425
426 list->list = g_list_concat(list->list, g_list_reverse(data.entries));
427 return list;
428}
429
430filter_list_t *
431ws_filter_list_read_own(filter_list_type_t list_type, const char* app_env_var_prefix)
432{
433 return filter_list_read_own(list_type, app_env_var_prefix, PROFILE_INHERIT_PROFILE_ONLY);
1
Calling 'filter_list_read_own'
434}
435
436/*
437 * Add a new filter to the end of a list.
438 */
439void
440ws_filter_list_add(filter_list_t *fl, const char *name,
441 const char *expression)
442{
443 fl->list = add_filter_entry(fl->list, name, expression);
444}
445
446static int
447compare_def(const void *def, const void *name)
448{
449 return g_strcmp0(((filter_def *)def)->name, name);
450}
451
452GList *ws_filter_list_find(filter_list_t *list, const char *name)
453{
454 return g_list_find_custom(list->list, name, compare_def);
455}
456
457/*
458 * Remove a filter from a list.
459 */
460bool_Bool
461ws_filter_list_remove(filter_list_t *list, const char *name)
462{
463 GList *p;
464
465 p = g_list_find_custom(list->list, name, compare_def);
466 if (p == NULL((void*)0))
467 return false0;
468 list->list = remove_filter_entry(list->list, p);
469 return true1;
470}
471
472/*
473 * Write out a list of filters.
474 *
475 * On error, report the error via the UI.
476 */
477void
478ws_filter_list_write(filter_list_t *list, const char* app_env_var_prefix)
479{
480 char *pf_dir_path;
481 const char *ff_name, *ff_description;
482 char *ff_path, *ff_path_new;
483 GList *fl;
484 GList *flpp;
485 filter_def *filt;
486 FILE *ff;
487 unsigned char *p, c;
488
489 ff_name = filter_list_file_name(list->type, &ff_description);
490 fl = list->list;
491
492 /* Create the directory that holds personal configuration files,
493 if necessary. */
494 if (create_persconffile_dir(app_env_var_prefix, &pf_dir_path) == -1) {
495 report_failure("Can't create directory\n\"%s\"\nfor filter files: %s.",
496 pf_dir_path, g_strerror(errno(*__errno_location ())));
497 g_free(pf_dir_path)(__builtin_object_size ((pf_dir_path), 0) != ((size_t) - 1)) ?
g_free_sized (pf_dir_path, __builtin_object_size ((pf_dir_path
), 0)) : (g_free) (pf_dir_path)
;
498 return;
499 }
500
501 ff_path = get_persconffile_path(ff_name, true1, app_env_var_prefix);
502
503 /* Write to "XXX.new", and rename if that succeeds.
504 That means we don't trash the file if we fail to write it out
505 completely. */
506 ff_path_new = ws_strdup_printf("%s.new", ff_path)wmem_strdup_printf(((void*)0), "%s.new", ff_path);
507
508 if ((ff = ws_fopenfopen(ff_path_new, "w")) == NULL((void*)0)) {
509 /* We had an error saving the filter. */
510 report_failure("Error saving your %s file\nCouldn't open \"%s\": %s.",
511 ff_description, ff_path_new, g_strerror(errno(*__errno_location ())));
512 g_free(ff_path_new)(__builtin_object_size ((ff_path_new), 0) != ((size_t) - 1)) ?
g_free_sized (ff_path_new, __builtin_object_size ((ff_path_new
), 0)) : (g_free) (ff_path_new)
;
513 g_free(ff_path)(__builtin_object_size ((ff_path), 0) != ((size_t) - 1)) ? g_free_sized
(ff_path, __builtin_object_size ((ff_path), 0)) : (g_free) (
ff_path)
;
514 return;
515 }
516 flpp = g_list_first(fl);
517 while (flpp) {
518 filt = (filter_def *) flpp->data;
519
520 /* Filters inherited from parent profiles are not written to this profile */
521 if (filt->source_profile != NULL((void*)0)) {
522 flpp = flpp->next;
523 continue;
524 }
525
526 /* Write out the filter name as a quoted string; escape any quotes
527 or backslashes. */
528 putc('"', ff);
529 for (p = (unsigned char *)filt->name; (c = *p) != '\0'; p++) {
530 if (c == '"' || c == '\\')
531 putc('\\', ff);
532 putc(c, ff);
533 }
534 putc('"', ff);
535
536 /* Separate the filter name and value with a space. */
537 putc(' ', ff);
538
539 /* Write out the filter expression and a newline. */
540 fprintf(ff, "%s\n", filt->strval);
541 if (ferror(ff)) {
542 report_failure("Error saving your %s file\nWrite to \"%s\" failed: %s.",
543 ff_description, ff_path_new, g_strerror(errno(*__errno_location ())));
544 fclose(ff);
545 ws_unlinkunlink(ff_path_new);
546 g_free(ff_path_new)(__builtin_object_size ((ff_path_new), 0) != ((size_t) - 1)) ?
g_free_sized (ff_path_new, __builtin_object_size ((ff_path_new
), 0)) : (g_free) (ff_path_new)
;
547 g_free(ff_path)(__builtin_object_size ((ff_path), 0) != ((size_t) - 1)) ? g_free_sized
(ff_path, __builtin_object_size ((ff_path), 0)) : (g_free) (
ff_path)
;
548 return;
549 }
550 flpp = flpp->next;
551 }
552 if (fclose(ff) == EOF(-1)) {
553 report_failure("Error saving your %s file\nWrite to \"%s\" failed: %s.",
554 ff_description, ff_path_new, g_strerror(errno(*__errno_location ())));
555 ws_unlinkunlink(ff_path_new);
556 g_free(ff_path_new)(__builtin_object_size ((ff_path_new), 0) != ((size_t) - 1)) ?
g_free_sized (ff_path_new, __builtin_object_size ((ff_path_new
), 0)) : (g_free) (ff_path_new)
;
557 g_free(ff_path)(__builtin_object_size ((ff_path), 0) != ((size_t) - 1)) ? g_free_sized
(ff_path, __builtin_object_size ((ff_path), 0)) : (g_free) (
ff_path)
;
558 return;
559 }
560
561#ifdef _WIN32
562 /* ANSI C doesn't say whether "rename()" removes the target if it
563 exists; the Win32 call to rename files doesn't do so, which I
564 infer is the reason why the MSVC++ "rename()" doesn't do so.
565 We must therefore remove the target file first, on Windows.
566
567 XXX - ws_rename() is g_rename() on Windows, and g_rename()
568 uses MoveFileEx() with MOVEFILE_REPLACE_EXISTING,
569 so it should remove the target if it exists, so this stuff
570 shouldn't be necessary. Perhaps it dates back to when we were
571 calling rename(), with that being a wrapper around Microsoft's
572 _rename(), which didn't remove the target. */
573 if (ws_removeremove(ff_path) < 0 && errno(*__errno_location ()) != ENOENT2) {
574 /* It failed for some reason other than "it's not there"; if
575 it's not there, we don't need to remove it, so we just
576 drive on. */
577 report_failure("Error saving your %s file\nCouldn't remove \"%s\": %s.",
578 ff_description, ff_path, g_strerror(errno(*__errno_location ())));
579 ws_unlinkunlink(ff_path_new);
580 g_free(ff_path_new)(__builtin_object_size ((ff_path_new), 0) != ((size_t) - 1)) ?
g_free_sized (ff_path_new, __builtin_object_size ((ff_path_new
), 0)) : (g_free) (ff_path_new)
;
581 g_free(ff_path)(__builtin_object_size ((ff_path), 0) != ((size_t) - 1)) ? g_free_sized
(ff_path, __builtin_object_size ((ff_path), 0)) : (g_free) (
ff_path)
;
582 return;
583 }
584#endif
585
586 if (ws_renamerename(ff_path_new, ff_path) < 0) {
587 report_failure("Error saving your %s file\nCouldn't rename \"%s\" to \"%s\": %s.",
588 ff_description, ff_path_new, ff_path, g_strerror(errno(*__errno_location ())));
589 ws_unlinkunlink(ff_path_new);
590 g_free(ff_path_new)(__builtin_object_size ((ff_path_new), 0) != ((size_t) - 1)) ?
g_free_sized (ff_path_new, __builtin_object_size ((ff_path_new
), 0)) : (g_free) (ff_path_new)
;
591 g_free(ff_path)(__builtin_object_size ((ff_path), 0) != ((size_t) - 1)) ? g_free_sized
(ff_path, __builtin_object_size ((ff_path), 0)) : (g_free) (
ff_path)
;
592 return;
593 }
594 g_free(ff_path_new)(__builtin_object_size ((ff_path_new), 0) != ((size_t) - 1)) ?
g_free_sized (ff_path_new, __builtin_object_size ((ff_path_new
), 0)) : (g_free) (ff_path_new)
;
595 g_free(ff_path)(__builtin_object_size ((ff_path), 0) != ((size_t) - 1)) ? g_free_sized
(ff_path, __builtin_object_size ((ff_path), 0)) : (g_free) (
ff_path)
;
596}