6 extern void abort (void);
8 extern int inside_main
;
9 void *chk_fail_buf
[256] __attribute__((aligned (16)));
10 volatile int chk_fail_allowed
, chk_calls
;
11 volatile int memcpy_disallowed
, mempcpy_disallowed
, memmove_disallowed
;
12 volatile int memset_disallowed
, strcpy_disallowed
, stpcpy_disallowed
;
13 volatile int strncpy_disallowed
, stpncpy_disallowed
, strcat_disallowed
;
14 volatile int strncat_disallowed
, sprintf_disallowed
, vsprintf_disallowed
;
15 volatile int snprintf_disallowed
, vsnprintf_disallowed
;
16 extern __SIZE_TYPE__
strlen (const char *);
17 extern int vsprintf (char *, const char *, va_list);
19 void __attribute__((noreturn
))
23 __builtin_longjmp (chk_fail_buf
, 1);
28 memcpy (void *dst
, const void *src
, __SIZE_TYPE__ n
)
34 if (memcpy_disallowed
&& inside_main
)
47 __memcpy_chk (void *dst
, const void *src
, __SIZE_TYPE__ n
, __SIZE_TYPE__ size
)
49 /* If size is -1, GCC should always optimize the call into memcpy. */
50 if (size
== (__SIZE_TYPE__
) -1)
55 return memcpy (dst
, src
, n
);
59 mempcpy (void *dst
, const void *src
, __SIZE_TYPE__ n
)
65 if (mempcpy_disallowed
&& inside_main
)
78 __mempcpy_chk (void *dst
, const void *src
, __SIZE_TYPE__ n
, __SIZE_TYPE__ size
)
80 /* If size is -1, GCC should always optimize the call into mempcpy. */
81 if (size
== (__SIZE_TYPE__
) -1)
86 return mempcpy (dst
, src
, n
);
90 memmove (void *dst
, const void *src
, __SIZE_TYPE__ n
)
96 if (memmove_disallowed
&& inside_main
)
113 __memmove_chk (void *dst
, const void *src
, __SIZE_TYPE__ n
, __SIZE_TYPE__ size
)
115 /* If size is -1, GCC should always optimize the call into memmove. */
116 if (size
== (__SIZE_TYPE__
) -1)
121 return memmove (dst
, src
, n
);
125 memset (void *dst
, int c
, __SIZE_TYPE__ n
)
127 /* Single-byte memsets should be done inline when optimisation
130 if (memset_disallowed
&& inside_main
&& n
< 2)
141 __memset_chk (void *dst
, int c
, __SIZE_TYPE__ n
, __SIZE_TYPE__ size
)
143 /* If size is -1, GCC should always optimize the call into memset. */
144 if (size
== (__SIZE_TYPE__
) -1)
149 return memset (dst
, c
, n
);
153 strcpy (char *d
, const char *s
)
157 if (strcpy_disallowed
&& inside_main
)
160 while ((*d
++ = *s
++));
165 __strcpy_chk (char *d
, const char *s
, __SIZE_TYPE__ size
)
167 /* If size is -1, GCC should always optimize the call into strcpy. */
168 if (size
== (__SIZE_TYPE__
) -1)
171 if (strlen (s
) >= size
)
173 return strcpy (d
, s
);
177 stpcpy (char *dst
, const char *src
)
180 if (stpcpy_disallowed
&& inside_main
)
192 __stpcpy_chk (char *d
, const char *s
, __SIZE_TYPE__ size
)
194 /* If size is -1, GCC should always optimize the call into stpcpy. */
195 if (size
== (__SIZE_TYPE__
) -1)
198 if (strlen (s
) >= size
)
200 return stpcpy (d
, s
);
204 stpncpy (char *dst
, const char *src
, __SIZE_TYPE__ n
)
207 if (stpncpy_disallowed
&& inside_main
)
211 for (; *src
&& n
; n
--)
224 __stpncpy_chk (char *s1
, const char *s2
, __SIZE_TYPE__ n
, __SIZE_TYPE__ size
)
226 /* If size is -1, GCC should always optimize the call into stpncpy. */
227 if (size
== (__SIZE_TYPE__
) -1)
232 return stpncpy (s1
, s2
, n
);
236 strncpy (char *s1
, const char *s2
, __SIZE_TYPE__ n
)
240 if (strncpy_disallowed
&& inside_main
)
243 for (; *s2
&& n
; n
--)
251 __strncpy_chk (char *s1
, const char *s2
, __SIZE_TYPE__ n
, __SIZE_TYPE__ size
)
253 /* If size is -1, GCC should always optimize the call into strncpy. */
254 if (size
== (__SIZE_TYPE__
) -1)
259 return strncpy (s1
, s2
, n
);
263 strcat (char *dst
, const char *src
)
268 if (strcat_disallowed
&& inside_main
)
274 while ((*p
++ = *src
++))
280 __strcat_chk (char *d
, const char *s
, __SIZE_TYPE__ size
)
282 /* If size is -1, GCC should always optimize the call into strcat. */
283 if (size
== (__SIZE_TYPE__
) -1)
286 if (strlen (d
) + strlen (s
) >= size
)
288 return strcat (d
, s
);
292 strncat (char *s1
, const char *s2
, __SIZE_TYPE__ n
)
297 if (strncat_disallowed
&& inside_main
)
316 __strncat_chk (char *d
, const char *s
, __SIZE_TYPE__ n
, __SIZE_TYPE__ size
)
318 __SIZE_TYPE__ len
= strlen (d
), n1
= n
;
321 /* If size is -1, GCC should always optimize the call into strncat. */
322 if (size
== (__SIZE_TYPE__
) -1)
325 while (len
< size
&& n1
> 0)
335 return strncat (d
, s
, n
);
338 /* No chk test in GCC testsuite needs more bytes than this.
339 As we can't expect vsnprintf to be available on the target,
340 assume 4096 bytes is enough. */
341 static char chk_sprintf_buf
[4096];
344 __sprintf_chk (char *str
, int flag
, __SIZE_TYPE__ size
, const char *fmt
, ...)
349 /* If size is -1 and flag 0, GCC should always optimize the call into
351 if (size
== (__SIZE_TYPE__
) -1 && flag
== 0)
355 if (sprintf_disallowed
&& inside_main
)
359 ret
= vsprintf (chk_sprintf_buf
, fmt
, ap
);
365 memcpy (str
, chk_sprintf_buf
, ret
+ 1);
371 __vsprintf_chk (char *str
, int flag
, __SIZE_TYPE__ size
, const char *fmt
,
376 /* If size is -1 and flag 0, GCC should always optimize the call into
378 if (size
== (__SIZE_TYPE__
) -1 && flag
== 0)
382 if (vsprintf_disallowed
&& inside_main
)
385 ret
= vsprintf (chk_sprintf_buf
, fmt
, ap
);
390 memcpy (str
, chk_sprintf_buf
, ret
+ 1);
396 __snprintf_chk (char *str
, __SIZE_TYPE__ len
, int flag
, __SIZE_TYPE__ size
,
397 const char *fmt
, ...)
402 /* If size is -1 and flag 0, GCC should always optimize the call into
404 if (size
== (__SIZE_TYPE__
) -1 && flag
== 0)
410 if (snprintf_disallowed
&& inside_main
)
414 ret
= vsprintf (chk_sprintf_buf
, fmt
, ap
);
419 memcpy (str
, chk_sprintf_buf
, ret
+ 1);
422 memcpy (str
, chk_sprintf_buf
, len
- 1);
430 __vsnprintf_chk (char *str
, __SIZE_TYPE__ len
, int flag
, __SIZE_TYPE__ size
,
431 const char *fmt
, va_list ap
)
435 /* If size is -1 and flag 0, GCC should always optimize the call into
437 if (size
== (__SIZE_TYPE__
) -1 && flag
== 0)
443 if (vsnprintf_disallowed
&& inside_main
)
446 ret
= vsprintf (chk_sprintf_buf
, fmt
, ap
);
450 memcpy (str
, chk_sprintf_buf
, ret
+ 1);
453 memcpy (str
, chk_sprintf_buf
, len
- 1);
461 snprintf (char *str
, __SIZE_TYPE__ len
, const char *fmt
, ...)
467 if (snprintf_disallowed
&& inside_main
)
471 ret
= vsprintf (chk_sprintf_buf
, fmt
, ap
);
476 memcpy (str
, chk_sprintf_buf
, ret
+ 1);
479 memcpy (str
, chk_sprintf_buf
, len
- 1);
486 /* uClibc's vsprintf calls vsnprintf. */
489 vsnprintf (char *str
, __SIZE_TYPE__ len
, const char *fmt
, va_list ap
)
494 if (vsnprintf_disallowed
&& inside_main
)
497 ret
= vsprintf (chk_sprintf_buf
, fmt
, ap
);
501 memcpy (str
, chk_sprintf_buf
, ret
+ 1);
504 memcpy (str
, chk_sprintf_buf
, len
- 1);