Debian Codegraph @

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