| 1 |
/* Integration test to ensure we issue a FILE * leak diagnostic for |
| 2 |
this particular non-trivial case. |
| 3 |
Adapted from intl/localealias.c, with all #includes removed. */ |
| 4 |
|
| 5 |
/* { dg-do "compile" } */ |
| 6 |
/* { dg-additional-options "-Wno-old-style-definition -Wno-analyzer-too-complex" } */ |
| 7 |
/* TODO: remove the need for this option. */ |
| 8 |
|
| 9 |
/* Handle aliases for locale names. |
| 10 |
Copyright (C) 1995-1999, 2000-2001, 2003 Free Software Foundation, Inc. |
| 11 |
|
| 12 |
This program is free software; you can redistribute it and/or modify it |
| 13 |
under the terms of the GNU Library General Public License as published |
| 14 |
by the Free Software Foundation; either version 2, or (at your option) |
| 15 |
any later version. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, |
| 18 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 |
Library General Public License for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU Library General Public |
| 23 |
License along with this program; if not, write to the Free Software |
| 24 |
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, |
| 25 |
USA. */ |
| 26 |
|
| 27 |
/* Minimal version of system headers. */ |
| 28 |
|
| 29 |
typedef __SIZE_TYPE__ size_t; |
| 30 |
#define NULL ((void *) 0) |
| 31 |
|
| 32 |
#define PATH_SEPARATOR ':' |
| 33 |
typedef struct _IO_FILE FILE; |
| 34 |
extern FILE *fopen(const char *__restrict __filename, |
| 35 |
const char *__restrict __modes); |
| 36 |
extern int feof_unlocked(FILE *__stream) __attribute__((__nothrow__, __leaf__)); |
| 37 |
extern char *fgets_unlocked(char *__restrict __s, int __n, |
| 38 |
FILE *__restrict __stream); |
| 39 |
extern int fclose(FILE *__stream); |
| 40 |
|
| 41 |
#define alloca __builtin_alloca |
| 42 |
|
| 43 |
extern char *strchr(const char *__s, int __c) |
| 44 |
__attribute__((__nothrow__, __leaf__)) __attribute__((__pure__)) |
| 45 |
__attribute__((__nonnull__(1))); |
| 46 |
extern void *memcpy(void *__restrict __dest, const void *__restrict __src, |
| 47 |
size_t __n) __attribute__((__nothrow__, __leaf__)) |
| 48 |
__attribute__((__nonnull__(1, 2))); |
| 49 |
extern void *mempcpy(void *__restrict __dest, const void *__restrict __src, |
| 50 |
size_t __n) __attribute__((__nothrow__, __leaf__)) |
| 51 |
__attribute__((__nonnull__(1, 2))); |
| 52 |
#define HAVE_MEMPCPY 1 |
| 53 |
extern size_t strlen(const char *__s) __attribute__((__nothrow__, __leaf__)) |
| 54 |
__attribute__((__pure__)) __attribute__((__nonnull__(1))); |
| 55 |
|
| 56 |
extern int strcasecmp(const char *__s1, const char *__s2) |
| 57 |
__attribute__((__nothrow__, __leaf__)) __attribute__((__pure__)) |
| 58 |
__attribute__((__nonnull__(1, 2))); |
| 59 |
|
| 60 |
extern int isspace(int) __attribute__((__nothrow__, __leaf__)); |
| 61 |
|
| 62 |
extern void *realloc(void *__ptr, size_t __size) |
| 63 |
__attribute__((__nothrow__, __leaf__)) |
| 64 |
__attribute__((__warn_unused_result__)); |
| 65 |
|
| 66 |
typedef int (*__compar_fn_t)(const void *, const void *); |
| 67 |
extern void *bsearch(const void *__key, const void *__base, size_t __nmemb, |
| 68 |
size_t __size, __compar_fn_t __compar) |
| 69 |
__attribute__((__nonnull__(1, 2, 5))); |
| 70 |
|
| 71 |
extern __inline __attribute__((__gnu_inline__)) void * |
| 72 |
bsearch(const void *__key, const void *__base, size_t __nmemb, size_t __size, |
| 73 |
__compar_fn_t __compar) { |
| 74 |
size_t __l, __u, __idx; |
| 75 |
const void *__p; |
| 76 |
int __comparison; |
| 77 |
|
| 78 |
__l = 0; |
| 79 |
__u = __nmemb; |
| 80 |
while (__l < __u) { |
| 81 |
__idx = (__l + __u) / 2; |
| 82 |
__p = (void *)(((const char *)__base) + (__idx * __size)); |
| 83 |
__comparison = (*__compar)(__key, __p); |
| 84 |
if (__comparison < 0) |
| 85 |
__u = __idx; |
| 86 |
else if (__comparison > 0) |
| 87 |
__l = __idx + 1; |
| 88 |
else |
| 89 |
return (void *)__p; |
| 90 |
} |
| 91 |
|
| 92 |
return ((void *)0); |
| 93 |
} |
| 94 |
|
| 95 |
extern void qsort(void *__base, size_t __nmemb, size_t __size, |
| 96 |
__compar_fn_t __compar) __attribute__((__nonnull__(1, 4))); |
| 97 |
|
| 98 |
/* Minimal version of intl headers. */ |
| 99 |
|
| 100 |
#define PARAMS(args) args |
| 101 |
|
| 102 |
#define relocate libintl_relocate |
| 103 |
extern const char *libintl_relocate(const char *pathname); |
| 104 |
|
| 105 |
#define LOCALE_ALIAS_PATH "value for LOCALE_ALIAS_PATH" |
| 106 |
|
| 107 |
/* Cleaned-up body of localealias.c follows. */ |
| 108 |
|
| 109 |
#ifndef internal_function |
| 110 |
# define internal_function |
| 111 |
#endif |
| 112 |
|
| 113 |
/* Some optimizations for glibc. */ |
| 114 |
# define FEOF(fp) feof_unlocked (fp) |
| 115 |
# define FGETS(buf, n, fp) fgets_unlocked (buf, n, fp) |
| 116 |
|
| 117 |
/* For those losing systems which don't have `alloca' we have to add |
| 118 |
some additional code emulating it. */ |
| 119 |
# define freea(p) /* nothing */ |
| 120 |
|
| 121 |
struct alias_map |
| 122 |
{ |
| 123 |
const char *alias; |
| 124 |
const char *value; |
| 125 |
}; |
| 126 |
|
| 127 |
# define libc_freeres_ptr(decl) decl |
| 128 |
|
| 129 |
libc_freeres_ptr (static char *string_space); |
| 130 |
static size_t string_space_act; |
| 131 |
static size_t string_space_max; |
| 132 |
libc_freeres_ptr (static struct alias_map *map); |
| 133 |
static size_t nmap; |
| 134 |
static size_t maxmap; |
| 135 |
|
| 136 |
|
| 137 |
/* Prototypes for local functions. */ |
| 138 |
static size_t read_alias_file PARAMS ((const char *fname, int fname_len)) |
| 139 |
internal_function; |
| 140 |
static int extend_alias_table PARAMS ((void)); |
| 141 |
static int alias_compare PARAMS ((const struct alias_map *map1, |
| 142 |
const struct alias_map *map2)); |
| 143 |
|
| 144 |
|
| 145 |
const char * |
| 146 |
_nl_expand_alias (name) |
| 147 |
const char *name; |
| 148 |
{ |
| 149 |
static const char *locale_alias_path; |
| 150 |
struct alias_map *retval; |
| 151 |
const char *result = NULL; |
| 152 |
size_t added; |
| 153 |
|
| 154 |
#ifdef _LIBC |
| 155 |
__libc_lock_lock (lock); |
| 156 |
#endif |
| 157 |
|
| 158 |
if (locale_alias_path == NULL) |
| 159 |
locale_alias_path = LOCALE_ALIAS_PATH; |
| 160 |
|
| 161 |
do |
| 162 |
{ |
| 163 |
struct alias_map item; |
| 164 |
|
| 165 |
item.alias = name; |
| 166 |
|
| 167 |
if (nmap > 0) |
| 168 |
retval = (struct alias_map *) bsearch (&item, map, nmap, |
| 169 |
sizeof (struct alias_map), |
| 170 |
(int (*) PARAMS ((const void *, |
| 171 |
const void *)) |
| 172 |
) alias_compare); |
| 173 |
else |
| 174 |
retval = NULL; |
| 175 |
|
| 176 |
/* We really found an alias. Return the value. */ |
| 177 |
if (retval != NULL) |
| 178 |
{ |
| 179 |
result = retval->value; |
| 180 |
break; |
| 181 |
} |
| 182 |
|
| 183 |
/* Perhaps we can find another alias file. */ |
| 184 |
added = 0; |
| 185 |
while (added == 0 && locale_alias_path[0] != '\0') |
| 186 |
{ |
| 187 |
const char *start; |
| 188 |
|
| 189 |
while (locale_alias_path[0] == PATH_SEPARATOR) |
| 190 |
++locale_alias_path; |
| 191 |
start = locale_alias_path; |
| 192 |
|
| 193 |
while (locale_alias_path[0] != '\0' |
| 194 |
&& locale_alias_path[0] != PATH_SEPARATOR) |
| 195 |
++locale_alias_path; |
| 196 |
|
| 197 |
if (start < locale_alias_path) |
| 198 |
added = read_alias_file (start, locale_alias_path - start); |
| 199 |
} |
| 200 |
} |
| 201 |
while (added != 0); |
| 202 |
|
| 203 |
#ifdef _LIBC |
| 204 |
__libc_lock_unlock (lock); |
| 205 |
#endif |
| 206 |
|
| 207 |
return result; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
static size_t |
| 212 |
internal_function |
| 213 |
read_alias_file (fname, fname_len) |
| 214 |
const char *fname; |
| 215 |
int fname_len; |
| 216 |
{ |
| 217 |
FILE *fp; |
| 218 |
char *full_fname; |
| 219 |
size_t added; |
| 220 |
static const char aliasfile[] = "/locale.alias"; |
| 221 |
|
| 222 |
full_fname = (char *) alloca (fname_len + sizeof aliasfile); |
| 223 |
#ifdef HAVE_MEMPCPY |
| 224 |
mempcpy (mempcpy (full_fname, fname, fname_len), |
| 225 |
aliasfile, sizeof aliasfile); |
| 226 |
#else |
| 227 |
memcpy (full_fname, fname, fname_len); |
| 228 |
memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile); |
| 229 |
#endif |
| 230 |
|
| 231 |
fp = fopen (relocate (full_fname), "r"); /* { dg-message "opened here" } */ |
| 232 |
freea (full_fname); |
| 233 |
if (fp == NULL) |
| 234 |
return 0; |
| 235 |
|
| 236 |
#ifdef HAVE___FSETLOCKING |
| 237 |
/* No threads present. */ |
| 238 |
__fsetlocking (fp, FSETLOCKING_BYCALLER); |
| 239 |
#endif |
| 240 |
|
| 241 |
added = 0; |
| 242 |
while (!FEOF (fp)) |
| 243 |
{ |
| 244 |
/* It is a reasonable approach to use a fix buffer here because |
| 245 |
a) we are only interested in the first two fields |
| 246 |
b) these fields must be usable as file names and so must not |
| 247 |
be that long |
| 248 |
We avoid a multi-kilobyte buffer here since this would use up |
| 249 |
stack space which we might not have if the program ran out of |
| 250 |
memory. */ |
| 251 |
char buf[400]; |
| 252 |
char *alias; |
| 253 |
char *value; |
| 254 |
char *cp; |
| 255 |
|
| 256 |
if (FGETS (buf, sizeof buf, fp) == NULL) |
| 257 |
/* EOF reached. */ |
| 258 |
break; |
| 259 |
|
| 260 |
cp = buf; |
| 261 |
/* Ignore leading white space. */ |
| 262 |
while (isspace ((unsigned char) cp[0])) |
| 263 |
++cp; |
| 264 |
|
| 265 |
/* A leading '#' signals a comment line. */ |
| 266 |
if (cp[0] != '\0' && cp[0] != '#') |
| 267 |
{ |
| 268 |
alias = cp++; |
| 269 |
while (cp[0] != '\0' && !isspace ((unsigned char) cp[0])) |
| 270 |
++cp; |
| 271 |
/* Terminate alias name. */ |
| 272 |
if (cp[0] != '\0') |
| 273 |
*cp++ = '\0'; |
| 274 |
|
| 275 |
/* Now look for the beginning of the value. */ |
| 276 |
while (isspace ((unsigned char) cp[0])) |
| 277 |
++cp; |
| 278 |
|
| 279 |
if (cp[0] != '\0') |
| 280 |
{ |
| 281 |
size_t alias_len; |
| 282 |
size_t value_len; |
| 283 |
|
| 284 |
value = cp++; |
| 285 |
while (cp[0] != '\0' && !isspace ((unsigned char) cp[0])) |
| 286 |
++cp; |
| 287 |
/* Terminate value. */ |
| 288 |
if (cp[0] == '\n') |
| 289 |
{ |
| 290 |
/* This has to be done to make the following test |
| 291 |
for the end of line possible. We are looking for |
| 292 |
the terminating '\n' which do not overwrite here. */ |
| 293 |
*cp++ = '\0'; |
| 294 |
*cp = '\n'; |
| 295 |
} |
| 296 |
else if (cp[0] != '\0') |
| 297 |
*cp++ = '\0'; |
| 298 |
|
| 299 |
if (nmap >= maxmap) |
| 300 |
if (__builtin_expect (extend_alias_table (), 0)) |
| 301 |
return added; /* { dg-warning "leak of FILE 'fp'" } */ |
| 302 |
|
| 303 |
alias_len = strlen (alias) + 1; |
| 304 |
value_len = strlen (value) + 1; |
| 305 |
|
| 306 |
if (string_space_act + alias_len + value_len > string_space_max) |
| 307 |
{ |
| 308 |
/* Increase size of memory pool. */ |
| 309 |
size_t new_size = (string_space_max |
| 310 |
+ (alias_len + value_len > 1024 |
| 311 |
? alias_len + value_len : 1024)); |
| 312 |
char *new_pool = (char *) realloc (string_space, new_size); |
| 313 |
if (new_pool == NULL) |
| 314 |
return added; |
| 315 |
|
| 316 |
if (__builtin_expect (string_space != new_pool, 0)) |
| 317 |
{ |
| 318 |
size_t i; |
| 319 |
|
| 320 |
for (i = 0; i < nmap; i++) |
| 321 |
{ |
| 322 |
map[i].alias += new_pool - string_space; |
| 323 |
map[i].value += new_pool - string_space; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
string_space = new_pool; |
| 328 |
string_space_max = new_size; |
| 329 |
} |
| 330 |
|
| 331 |
map[nmap].alias = memcpy (&string_space[string_space_act], |
| 332 |
alias, alias_len); |
| 333 |
string_space_act += alias_len; |
| 334 |
|
| 335 |
map[nmap].value = memcpy (&string_space[string_space_act], |
| 336 |
value, value_len); |
| 337 |
string_space_act += value_len; |
| 338 |
|
| 339 |
++nmap; |
| 340 |
++added; |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
/* Possibly not the whole line fits into the buffer. Ignore |
| 345 |
the rest of the line. */ |
| 346 |
while (strchr (buf, '\n') == NULL) |
| 347 |
if (FGETS (buf, sizeof buf, fp) == NULL) |
| 348 |
/* Make sure the inner loop will be left. The outer loop |
| 349 |
will exit at the `feof' test. */ |
| 350 |
break; |
| 351 |
} |
| 352 |
|
| 353 |
/* Should we test for ferror()? I think we have to silently ignore |
| 354 |
errors. --drepper */ |
| 355 |
fclose (fp); |
| 356 |
|
| 357 |
if (added > 0) |
| 358 |
qsort (map, nmap, sizeof (struct alias_map), |
| 359 |
(int (*) PARAMS ((const void *, const void *))) alias_compare); |
| 360 |
|
| 361 |
return added; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
static int |
| 366 |
extend_alias_table () |
| 367 |
{ |
| 368 |
size_t new_size; |
| 369 |
struct alias_map *new_map; |
| 370 |
|
| 371 |
new_size = maxmap == 0 ? 100 : 2 * maxmap; |
| 372 |
new_map = (struct alias_map *) realloc (map, (new_size |
| 373 |
* sizeof (struct alias_map))); |
| 374 |
if (new_map == NULL) |
| 375 |
/* Simply don't extend: we don't have any more core. */ |
| 376 |
return -1; |
| 377 |
|
| 378 |
map = new_map; |
| 379 |
maxmap = new_size; |
| 380 |
return 0; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
static int |
| 385 |
alias_compare (map1, map2) |
| 386 |
const struct alias_map *map1; |
| 387 |
const struct alias_map *map2; |
| 388 |
{ |
| 389 |
return strcasecmp (map1->alias, map2->alias); |
| 390 |
} |