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-27-100335-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/* A macro is found by its name, a filter also by its expression */
367static const char *
368filter_list_key2(filter_list_type_t list_type, const filter_def *filt)
369{
370 return list_type == DMACROS_LIST ? NULL((void*)0) : filt->strval;
371}
372
373/* Read the filters of one parent profile */
374static bool_Bool
375read_layer_filters(const profile_inherit_layer_t *layer, const char *path, const char *filename _U___attribute__((unused)),
376 profile_inherit_seen_t *seen, void *user_data, char **err)
377{
378 read_layer_data_t *data = (read_layer_data_t *)user_data;
379 FILE *ff;
380
381 if ((ff = ws_fopenfopen(path, "r")) == NULL((void*)0)) {
382 *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 (
))))
383 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 (
))))
;
384 return false0;
385 }
386 /* In the order of the file */
387 GList *layer_list = g_list_reverse(read_filter_file(ff, path, data->description, NULL((void*)0)));
388 fclose(ff);
389
390 for (GList *item = layer_list; item; item = g_list_next(item)((item) ? (((GList *)(item))->next) : ((void*)0))) {
391 filter_def *filt = (filter_def *)item->data;
392
393 if (profile_inherit_seen_check(seen, filt->name, filter_list_key2(data->type, filt))) {
394 free_filter_entry(filt);
395 continue;
396 }
397 filt->source_profile = g_strdup(layer->source)g_strdup_inline (layer->source);
398 data->entries = g_list_prepend(data->entries, filt);
399 }
400 g_list_free(layer_list);
401 return true1;
402}
403
404filter_list_t *
405ws_filter_list_read(filter_list_type_t list_type, const char* app_env_var_prefix)
406{
407 const char *ff_name, *ff_description;
408
409 ff_name = filter_list_file_name(list_type, &ff_description);
410 profile_inherit_e mode = profile_get_inherit(app_env_var_prefix, NULL((void*)0), ff_name);
411
412 filter_list_t *list = filter_list_read_own(list_type, app_env_var_prefix, mode);
413 if (mode == PROFILE_INHERIT_PROFILE_ONLY)
414 return list;
415
416 /* The profile's own filters come first, and win over duplicates from parents */
417 profile_inherit_seen_t *seen = profile_inherit_seen_new();
418 for (GList *item = list->list; item; item = g_list_next(item)((item) ? (((GList *)(item))->next) : ((void*)0))) {
419 const filter_def *filt = (const filter_def *)item->data;
420 profile_inherit_seen_check(seen, filt->name, filter_list_key2(list_type, filt));
421 }
422 profile_inherit_seen_next(seen);
423
424 read_layer_data_t data = { list_type, ff_description, NULL((void*)0) };
425 char *err = NULL((void*)0);
426 if (!profile_inherit_read_layers(app_env_var_prefix, ff_name, NULL((void*)0), true1, mode, seen,
427 read_layer_filters, &data, &err)) {
428 report_warning("%s", err);
429 }
430 g_free(err)(__builtin_object_size ((err), 0) != ((size_t) - 1)) ? g_free_sized
(err, __builtin_object_size ((err), 0)) : (g_free) (err)
;
431 profile_inherit_seen_free(seen);
432
433 list->list = g_list_concat(list->list, g_list_reverse(data.entries));
434 return list;
435}
436
437filter_list_t *
438ws_filter_list_read_own(filter_list_type_t list_type, const char* app_env_var_prefix)
439{
440 return filter_list_read_own(list_type, app_env_var_prefix, PROFILE_INHERIT_PROFILE_ONLY);
1
Calling 'filter_list_read_own'
441}
442
443/*
444 * Add a new filter to the end of a list.
445 */
446void
447ws_filter_list_add(filter_list_t *fl, const char *name,
448 const char *expression)
449{
450 fl->list = add_filter_entry(fl->list, name, expression);
451}
452
453static int
454compare_def(const void *def, const void *name)
455{
456 return g_strcmp0(((filter_def *)def)->name, name);
457}
458
459GList *ws_filter_list_find(filter_list_t *list, const char *name)
460{
461 return g_list_find_custom(list->list, name, compare_def);
462}
463
464/*
465 * Remove a filter from a list.
466 */
467bool_Bool
468ws_filter_list_remove(filter_list_t *list, const char *name)
469{
470 GList *p;
471
472 p = g_list_find_custom(list->list, name, compare_def);
473 if (p == NULL((void*)0))
474 return false0;
475 list->list = remove_filter_entry(list->list, p);
476 return true1;
477}
478
479/*
480 * Write out a list of filters.
481 *
482 * On error, report the error via the UI.
483 */
484void
485ws_filter_list_write(filter_list_t *list, const char* app_env_var_prefix)
486{
487 char *pf_dir_path;
488 const char *ff_name, *ff_description;
489 char *ff_path, *ff_path_new;
490 GList *fl;
491 GList *flpp;
492 filter_def *filt;
493 FILE *ff;
494 unsigned char *p, c;
495
496 ff_name = filter_list_file_name(list->type, &ff_description);
497 fl = list->list;
498
499 /* Create the directory that holds personal configuration files,
500 if necessary. */
501 if (create_persconffile_dir(app_env_var_prefix, &pf_dir_path) == -1) {
502 report_failure("Can't create directory\n\"%s\"\nfor filter files: %s.",
503 pf_dir_path, g_strerror(errno(*__errno_location ())));
504 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)
;
505 return;
506 }
507
508 ff_path = get_persconffile_path(ff_name, true1, app_env_var_prefix);
509
510 /* Write to "XXX.new", and rename if that succeeds.
511 That means we don't trash the file if we fail to write it out
512 completely. */
513 ff_path_new = ws_strdup_printf("%s.new", ff_path)wmem_strdup_printf(((void*)0), "%s.new", ff_path);
514
515 if ((ff = ws_fopenfopen(ff_path_new, "w")) == NULL((void*)0)) {
516 /* We had an error saving the filter. */
517 report_failure("Error saving your %s file\nCouldn't open \"%s\": %s.",
518 ff_description, ff_path_new, g_strerror(errno(*__errno_location ())));
519 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)
;
520 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)
;
521 return;
522 }
523 flpp = g_list_first(fl);
524 while (flpp) {
525 filt = (filter_def *) flpp->data;
526
527 /* Filters inherited from parent profiles are not written to this profile */
528 if (filt->source_profile != NULL((void*)0)) {
529 flpp = flpp->next;
530 continue;
531 }
532
533 /* Write out the filter name as a quoted string; escape any quotes
534 or backslashes. */
535 putc('"', ff);
536 for (p = (unsigned char *)filt->name; (c = *p) != '\0'; p++) {
537 if (c == '"' || c == '\\')
538 putc('\\', ff);
539 putc(c, ff);
540 }
541 putc('"', ff);
542
543 /* Separate the filter name and value with a space. */
544 putc(' ', ff);
545
546 /* Write out the filter expression and a newline. */
547 fprintf(ff, "%s\n", filt->strval);
548 if (ferror(ff)) {
549 report_failure("Error saving your %s file\nWrite to \"%s\" failed: %s.",
550 ff_description, ff_path_new, g_strerror(errno(*__errno_location ())));
551 fclose(ff);
552 ws_unlinkunlink(ff_path_new);
553 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)
;
554 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)
;
555 return;
556 }
557 flpp = flpp->next;
558 }
559 if (fclose(ff) == EOF(-1)) {
560 report_failure("Error saving your %s file\nWrite to \"%s\" failed: %s.",
561 ff_description, ff_path_new, g_strerror(errno(*__errno_location ())));
562 ws_unlinkunlink(ff_path_new);
563 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)
;
564 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)
;
565 return;
566 }
567
568#ifdef _WIN32
569 /* ANSI C doesn't say whether "rename()" removes the target if it
570 exists; the Win32 call to rename files doesn't do so, which I
571 infer is the reason why the MSVC++ "rename()" doesn't do so.
572 We must therefore remove the target file first, on Windows.
573
574 XXX - ws_rename() is g_rename() on Windows, and g_rename()
575 uses MoveFileEx() with MOVEFILE_REPLACE_EXISTING,
576 so it should remove the target if it exists, so this stuff
577 shouldn't be necessary. Perhaps it dates back to when we were
578 calling rename(), with that being a wrapper around Microsoft's
579 _rename(), which didn't remove the target. */
580 if (ws_removeremove(ff_path) < 0 && errno(*__errno_location ()) != ENOENT2) {
581 /* It failed for some reason other than "it's not there"; if
582 it's not there, we don't need to remove it, so we just
583 drive on. */
584 report_failure("Error saving your %s file\nCouldn't remove \"%s\": %s.",
585 ff_description, ff_path, g_strerror(errno(*__errno_location ())));
586 ws_unlinkunlink(ff_path_new);
587 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)
;
588 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)
;
589 return;
590 }
591#endif
592
593 if (ws_renamerename(ff_path_new, ff_path) < 0) {
594 report_failure("Error saving your %s file\nCouldn't rename \"%s\" to \"%s\": %s.",
595 ff_description, ff_path_new, ff_path, g_strerror(errno(*__errno_location ())));
596 ws_unlinkunlink(ff_path_new);
597 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)
;
598 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)
;
599 return;
600 }
601 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)
;
602 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)
;
603}