Debian Codegraph @

gcc/testsuite/gcc.dg/analyzer/pr93355-localealias-feasibility.c

raw
1 /* Simplified version of test to ensure we issue a FILE * leak diagnostic,
2 reproducing a feasibility issue.
3 Adapted from intl/localealias.c, with all #includes removed. */
4
5 /* { dg-do "compile" } */
6
7 /* C only: C++ exceptions cause another fopen leak warning to be emitted at line 54.
8 Therefore this test has been duplicated as
9 c-c++-common/analyzer/pr93355-localealias-feasibility-noexcept.c */
10
11 /* Handle aliases for locale names.
12 Copyright (C) 1995-1999, 2000-2001, 2003 Free Software Foundation, Inc.
13
14 This program is free software; you can redistribute it and/or modify it
15 under the terms of the GNU Library General Public License as published
16 by the Free Software Foundation; either version 2, or (at your option)
17 any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Library General Public License for more details.
23
24 You should have received a copy of the GNU Library General Public
25 License along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
27 USA. */
28
29
30 #include "../../gcc.dg/analyzer/analyzer-decls.h"
31 /* Minimal version of system headers. */
32 typedef __SIZE_TYPE__ size_t;
33
34 typedef struct _IO_FILE FILE;
35 extern FILE *fopen (const char *__restrict __filename,
36 const char *__restrict __modes);
37 extern size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
38 extern int fclose (FILE *__stream);
39
40 extern int isspace (int) __attribute__((__nothrow__, __leaf__));
41
42 /* Cleaned-up body of localealias.c follows. */
43
44 size_t
45 read_alias_file (const char *fname, int fname_len)
46 {
47 FILE *fp;
48 size_t added;
49 char buf[400];
50 char *alias;
51 char *value;
52 char *cp;
53
54 fp = fopen (fname, "r"); /* { dg-message "opened here" } */
55 if (fp == NULL)
56 return 0;
57
58 if (fread (buf, sizeof buf, 1, fp) != 1)
59 {
60 fclose (fp);
61 return 0;
62 }
63
64 cp = buf;
65
66 /* Ignore leading white space. */
67 while (isspace ((unsigned char)cp[0]))
68 ++cp;
69
70 if (cp[0] != '\0' && cp[0] != '#')
71 {
72 alias = cp++;
73 while (cp[0] != '\0' && !isspace ((unsigned char)cp[0]))
74 ++cp;
75 if (cp[0] != '\0')
76 *cp++ = '\0';
77
78 while (isspace ((unsigned char)cp[0]))
79 ++cp;
80
81 if (cp[0] != '\0')
82 return 42; /* { dg-warning "leak of FILE 'fp'" } */
83 }
84
85 fclose(fp);
86
87 return 0;
88 }