Bug Summary

File:builds/wireshark/wireshark/wsutil/filter_files.c
Warning:line 164, column 21
Read function called when stream is in EOF state. Function has no effect

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-30-100354-3660-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 <[email protected]>
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++) {
4
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))
5
Taking false branch
158 break; /* Nothing more to read */
159 if (c == '\n')
6
Taking false branch
160 continue; /* Blank line. */
161 if (c == '#') {
7
Assuming the condition is true
8
Taking true branch
162 /* Comment. */
163 while (c != '\n')
9
Loop condition is true. Entering loop body
11
Loop condition is true. Entering loop body
164 c = getc(ff); /* skip to the end of the line */
10
Assuming stream reaches end-of-file here
12
Read function called when stream is in EOF state. Function has no effect
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. Only a profile that does not include its parent profiles
295 * falls back to the global file.
296 */
297static filter_list_t *
298filter_list_read_own(filter_list_type_t list_type, const char* app_env_var_prefix, bool_Bool include_parent)
299{
300 const char *ff_name, *ff_description;
301 char *ff_path;
302 FILE *ff;
303
304 filter_list_t *list = g_new(filter_list_t, 1)((filter_list_t *) g_malloc_n ((1), sizeof (filter_list_t)));
305 list->type = list_type;
306 list->list = NULL((void*)0);
307
308 ff_name = filter_list_file_name(list_type, &ff_description);
309
310 /* try to open personal "cfilters"/"dfilters" file */
311 ff_path = get_persconffile_path(ff_name, true1, app_env_var_prefix);
312 if ((ff = ws_fopenfopen(ff_path, "r")) == NULL((void*)0)) {
2
Taking false branch
313 /*
314 * Did that fail because the file didn't exist?
315 */
316 if (errno(*__errno_location ()) != ENOENT2) {
317 /*
318 * No. Just give up.
319 */
320 report_warning("Could not open your %s file\n\"%s\": %s.",
321 ff_description, ff_path, g_strerror(errno(*__errno_location ())));
322 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)
;
323 return list;
324 }
325
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 if (include_parent) {
328 /* The global filters come from the parent profiles */
329 return list;
330 }
331
332 /*
333 * Yes. Try to open the global "cfilters/dfilters" file.
334 */
335 ff_path = get_datafile_path(ff_name, app_env_var_prefix);
336 if ((ff = ws_fopenfopen(ff_path, "r")) == NULL((void*)0)) {
337 /*
338 * Well, that didn't work, either. Just give up.
339 * Report an error if the file existed but we couldn't open it.
340 */
341 if (errno(*__errno_location ()) != ENOENT2) {
342 report_warning("Could not open your %s file\n\"%s\": %s.",
343 ff_description, ff_path, g_strerror(errno(*__errno_location ())));
344 }
345 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)
;
346 return list;
347 }
348 }
349
350 list->list = read_filter_file(ff, ff_path, ff_description, NULL((void*)0));
3
Calling 'read_filter_file'
351 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)
;
352 fclose(ff);
353 return list;
354}
355
356typedef struct {
357 filter_list_type_t type;
358 const char *description;
359 GList *entries; /* The inherited filters, in reverse order */
360} read_layer_data_t;
361
362/* A macro, and a capture filter used by name with "predef:", is found by its
363 * name, a display filter also by its expression */
364static const char *
365filter_list_key2(filter_list_type_t list_type, const filter_def *filt)
366{
367 return list_type == DFILTER_LIST ? filt->strval : NULL((void*)0);
368}
369
370/* Read the filters of one parent profile */
371static bool_Bool
372read_layer_filters(const profile_inherit_layer_t *layer, const char *path, const char *filename _U___attribute__((unused)),
373 profile_inherit_seen_t *seen, void *user_data, char **err)
374{
375 read_layer_data_t *data = (read_layer_data_t *)user_data;
376 FILE *ff;
377
378 if ((ff = ws_fopenfopen(path, "r")) == NULL((void*)0)) {
379 *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 (
))))
380 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 (
))))
;
381 return false0;
382 }
383 /* In the order of the file */
384 GList *layer_list = g_list_reverse(read_filter_file(ff, path, data->description, NULL((void*)0)));
385 fclose(ff);
386
387 for (GList *item = layer_list; item; item = g_list_next(item)((item) ? (((GList *)(item))->next) : ((void*)0))) {
388 filter_def *filt = (filter_def *)item->data;
389
390 if (profile_inherit_seen_check(seen, filt->name, filter_list_key2(data->type, filt))) {
391 free_filter_entry(filt);
392 continue;
393 }
394 filt->source_profile = g_strdup(layer->source)g_strdup_inline (layer->source);
395 data->entries = g_list_prepend(data->entries, filt);
396 }
397 g_list_free(layer_list);
398 return true1;
399}
400
401filter_list_t *
402ws_filter_list_read(filter_list_type_t list_type, const char* app_env_var_prefix)
403{
404 const char *ff_name, *ff_description;
405
406 ff_name = filter_list_file_name(list_type, &ff_description);
407 bool_Bool include_parent = profile_get_include_parent(app_env_var_prefix, NULL((void*)0), ff_name);
408
409 filter_list_t *list = filter_list_read_own(list_type, app_env_var_prefix, include_parent);
410 if (!include_parent)
411 return list;
412
413 /* The profile's own filters come first, and win over duplicates from parents */
414 profile_inherit_seen_t *seen = profile_inherit_seen_new();
415 for (GList *item = list->list; item; item = g_list_next(item)((item) ? (((GList *)(item))->next) : ((void*)0))) {
416 const filter_def *filt = (const filter_def *)item->data;
417 profile_inherit_seen_check(seen, filt->name, filter_list_key2(list_type, filt));
418 }
419 profile_inherit_seen_next(seen);
420
421 read_layer_data_t data = { list_type, ff_description, NULL((void*)0) };
422 char *err = NULL((void*)0);
423 if (!profile_inherit_read_layers(app_env_var_prefix, ff_name, NULL((void*)0), true1, true1, seen,
424 read_layer_filters, &data, &err)) {
425 report_warning("%s", err);
426 }
427 g_free(err)(__builtin_object_size ((err), 0) != ((size_t) - 1)) ? g_free_sized
(err, __builtin_object_size ((err), 0)) : (g_free) (err)
;
428 profile_inherit_seen_free(seen);
429
430 list->list = g_list_concat(list->list, g_list_reverse(data.entries));
431 return list;
432}
433
434filter_list_t *
435ws_filter_list_read_own(filter_list_type_t list_type, const char* app_env_var_prefix)
436{
437 /* Without a personal file, the global filters are the own ones, unless
438 * they come from the parent profiles */
439 const char *ff_description;
440 const char *ff_name = filter_list_file_name(list_type, &ff_description);
441 return filter_list_read_own(list_type, app_env_var_prefix,
1
Calling 'filter_list_read_own'
442 profile_get_include_parent(app_env_var_prefix, NULL((void*)0), ff_name));
443}
444
445/*
446 * Add a new filter to the end of a list.
447 */
448void
449ws_filter_list_add(filter_list_t *fl, const char *name,
450 const char *expression)
451{
452 fl->list = add_filter_entry(fl->list, name, expression);
453}
454
455static int
456compare_def(const void *def, const void *name)
457{
458 return g_strcmp0(((filter_def *)def)->name, name);
459}
460
461GList *ws_filter_list_find(filter_list_t *list, const char *name)
462{
463 return g_list_find_custom(list->list, name, compare_def);
464}
465
466/*
467 * Remove a filter from a list.
468 */
469bool_Bool
470ws_filter_list_remove(filter_list_t *list, const char *name)
471{
472 GList *p;
473
474 p = g_list_find_custom(list->list, name, compare_def);
475 if (p == NULL((void*)0))
476 return false0;
477 list->list = remove_filter_entry(list->list, p);
478 return true1;
479}
480
481/*
482 * Write out a list of filters.
483 *
484 * On error, report the error via the UI.
485 */
486void
487ws_filter_list_write(filter_list_t *list, const char* app_env_var_prefix)
488{
489 char *pf_dir_path;
490 const char *ff_name, *ff_description;
491 char *ff_path, *ff_path_new;
492 GList *fl;
493 GList *flpp;
494 filter_def *filt;
495 FILE *ff;
496 unsigned char *p, c;
497
498 ff_name = filter_list_file_name(list->type, &ff_description);
499 fl = list->list;
500
501 /* Create the directory that holds personal configuration files,
502 if necessary. */
503 if (create_persconffile_dir(app_env_var_prefix, &pf_dir_path) == -1) {
504 report_failure("Can't create directory\n\"%s\"\nfor filter files: %s.",
505 pf_dir_path, g_strerror(errno(*__errno_location ())));
506 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)
;
507 return;
508 }
509
510 ff_path = get_persconffile_path(ff_name, true1, app_env_var_prefix);
511
512 /* Write to "XXX.new", and rename if that succeeds.
513 That means we don't trash the file if we fail to write it out
514 completely. */
515 ff_path_new = ws_strdup_printf("%s.new", ff_path)wmem_strdup_printf(((void*)0), "%s.new", ff_path);
516
517 if ((ff = ws_fopenfopen(ff_path_new, "w")) == NULL((void*)0)) {
518 /* We had an error saving the filter. */
519 report_failure("Error saving your %s file\nCouldn't open \"%s\": %s.",
520 ff_description, ff_path_new, g_strerror(errno(*__errno_location ())));
521 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)
;
522 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)
;
523 return;
524 }
525 flpp = g_list_first(fl);
526 while (flpp) {
527 filt = (filter_def *) flpp->data;
528
529 /* Filters inherited from parent profiles are not written to this profile */
530 if (filt->source_profile != NULL((void*)0)) {
531 flpp = flpp->next;
532 continue;
533 }
534
535 /* Write out the filter name as a quoted string; escape any quotes
536 or backslashes. */
537 putc('"', ff);
538 for (p = (unsigned char *)filt->name; (c = *p) != '\0'; p++) {
539 if (c == '"' || c == '\\')
540 putc('\\', ff);
541 putc(c, ff);
542 }
543 putc('"', ff);
544
545 /* Separate the filter name and value with a space. */
546 putc(' ', ff);
547
548 /* Write out the filter expression and a newline. */
549 fprintf(ff, "%s\n", filt->strval);
550 if (ferror(ff)) {
551 report_failure("Error saving your %s file\nWrite to \"%s\" failed: %s.",
552 ff_description, ff_path_new, g_strerror(errno(*__errno_location ())));
553 fclose(ff);
554 ws_unlinkunlink(ff_path_new);
555 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)
;
556 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)
;
557 return;
558 }
559 flpp = flpp->next;
560 }
561 if (fclose(ff) == EOF(-1)) {
562 report_failure("Error saving your %s file\nWrite to \"%s\" failed: %s.",
563 ff_description, ff_path_new, g_strerror(errno(*__errno_location ())));
564 ws_unlinkunlink(ff_path_new);
565 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)
;
566 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)
;
567 return;
568 }
569
570#ifdef _WIN32
571 /* ANSI C doesn't say whether "rename()" removes the target if it
572 exists; the Win32 call to rename files doesn't do so, which I
573 infer is the reason why the MSVC++ "rename()" doesn't do so.
574 We must therefore remove the target file first, on Windows.
575
576 XXX - ws_rename() is g_rename() on Windows, and g_rename()
577 uses MoveFileEx() with MOVEFILE_REPLACE_EXISTING,
578 so it should remove the target if it exists, so this stuff
579 shouldn't be necessary. Perhaps it dates back to when we were
580 calling rename(), with that being a wrapper around Microsoft's
581 _rename(), which didn't remove the target. */
582 if (ws_removeremove(ff_path) < 0 && errno(*__errno_location ()) != ENOENT2) {
583 /* It failed for some reason other than "it's not there"; if
584 it's not there, we don't need to remove it, so we just
585 drive on. */
586 report_failure("Error saving your %s file\nCouldn't remove \"%s\": %s.",
587 ff_description, ff_path, g_strerror(errno(*__errno_location ())));
588 ws_unlinkunlink(ff_path_new);
589 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)
;
590 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)
;
591 return;
592 }
593#endif
594
595 if (ws_renamerename(ff_path_new, ff_path) < 0) {
596 report_failure("Error saving your %s file\nCouldn't rename \"%s\" to \"%s\": %s.",
597 ff_description, ff_path_new, ff_path, g_strerror(errno(*__errno_location ())));
598 ws_unlinkunlink(ff_path_new);
599 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)
;
600 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)
;
601 return;
602 }
603 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)
;
604 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)
;
605}