regexec.c: avoid arithmetic overflow in buffer size calculation
[glibc.git] / elf / dl-dst.h
blob76076a60366f8b76c46cef851dd40c94d390f685
1 /* Handling of dynamic sring tokens.
2 Copyright (C) 1999,2001,2002,2003,2004,2006,2007
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 /* Determine the number of DST elements in the name. Only if IS_PATH is
22 nonzero paths are recognized (i.e., multiple, ':' separated filenames). */
23 #define DL_DST_COUNT(name, is_path) \
24 ({ \
25 size_t __cnt = 0; \
26 const char *__sf = strchr (name, '$'); \
28 if (__builtin_expect (__sf != NULL, 0)) \
29 __cnt = _dl_dst_count (__sf, is_path); \
31 __cnt; })
34 /* Guess from the number of DSTs the length of the result string. */
35 #define DL_DST_REQUIRED(l, name, len, cnt) \
36 ({ \
37 size_t __len = (len); \
38 size_t __cnt = (cnt); \
40 if (__cnt > 0) \
41 { \
42 size_t origin_len; \
43 /* Now we make a guess how many extra characters on top of the \
44 length of S we need to represent the result. We know that \
45 we have CNT replacements. Each at most can use \
46 MAX (strlen (ORIGIN), strlen (_dl_platform)) \
47 minus 7 (which is the length of "$ORIGIN"). \
49 First get the origin string if it is not available yet. \
50 This can only happen for the map of the executable. */ \
51 DL_DST_REQ_STATIC (l) \
52 if ((l)->l_origin == NULL) \
53 { \
54 assert ((l)->l_name[0] == '\0'); \
55 (l)->l_origin = _dl_get_origin (); \
56 origin_len = ((l)->l_origin && (l)->l_origin != (char *) -1 \
57 ? strlen ((l)->l_origin) : 0); \
58 } \
59 else \
60 origin_len = (l)->l_origin == (char *) -1 \
61 ? 0 : strlen ((l)->l_origin); \
63 __len += __cnt * (MAX (origin_len, GLRO(dl_platformlen)) - 7); \
64 } \
66 __len; })
68 #ifdef SHARED
69 # define DL_DST_REQ_STATIC(l) /* nothing */
70 #else
71 # define DL_DST_REQ_STATIC(l) \
72 if ((l) == NULL) \
73 { \
74 const char *origin = _dl_get_origin (); \
75 origin_len = (origin && origin != (char *) -1 ? strlen (origin) : 0); \
76 } \
77 else
78 #endif