~ubuntu-branches/ubuntu/trusty/patch/trusty-security

« back to all changes in this revision

Viewing changes to lib/xalloc.h

  • Committer: Package Import Robot
  • Author(s): Christoph Berg
  • Date: 2013-01-03 17:34:45 UTC
  • mto: (6.1.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20130103173445-5vf8qmnfgd7ug67h
Tags: upstream-2.7.1
ImportĀ upstreamĀ versionĀ 2.7.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* xalloc.h -- malloc with out-of-memory checking
2
2
 
3
 
   Copyright (C) 1990-2000, 2003-2004, 2006-2011 Free Software Foundation, Inc.
 
3
   Copyright (C) 1990-2000, 2003-2004, 2006-2012 Free Software Foundation, Inc.
4
4
 
5
5
   This program is free software: you can redistribute it and/or modify
6
6
   it under the terms of the GNU General Public License as published by
20
20
 
21
21
# include <stddef.h>
22
22
 
 
23
# include "xalloc-oversized.h"
23
24
 
24
25
# ifdef __cplusplus
25
26
extern "C" {
26
27
# endif
27
28
 
28
29
 
29
 
# if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
30
 
#  define _GL_ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
31
 
# else
32
 
#  define _GL_ATTRIBUTE_NORETURN /* empty */
33
 
# endif
34
 
 
35
30
# if __GNUC__ >= 3
36
31
#  define _GL_ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
37
32
# else
49
44
   or by using gnulib's xalloc-die module.  This is the
50
45
   function to call when one wants the program to die because of a
51
46
   memory allocation failure.  */
52
 
extern void xalloc_die (void) _GL_ATTRIBUTE_NORETURN;
 
47
extern _Noreturn void xalloc_die (void);
53
48
 
54
49
void *xmalloc (size_t s)
55
50
      _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1));
65
60
char *xstrdup (char const *str)
66
61
      _GL_ATTRIBUTE_MALLOC;
67
62
 
68
 
/* Return 1 if an array of N objects, each of size S, cannot exist due
69
 
   to size arithmetic overflow.  S must be positive and N must be
70
 
   nonnegative.  This is a macro, not an inline function, so that it
71
 
   works correctly even when SIZE_MAX < N.
72
 
 
73
 
   By gnulib convention, SIZE_MAX represents overflow in size
74
 
   calculations, so the conservative dividend to use here is
75
 
   SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
76
 
   However, malloc (SIZE_MAX) fails on all known hosts where
77
 
   sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
78
 
   exactly-SIZE_MAX allocations on such hosts; this avoids a test and
79
 
   branch when S is known to be 1.  */
80
 
# define xalloc_oversized(n, s) \
81
 
    ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
82
 
 
83
 
 
84
63
/* In the following macros, T must be an elementary or structure/union or
85
64
   typedef'ed type, or a pointer to such a type.  To apply one of the
86
65
   following macros to a function pointer or array type, you need to typedef
213
192
        {
214
193
          /* The approximate size to use for initial small allocation
215
194
             requests, when the invoking code specifies an old size of
216
 
             zero.  64 bytes is the largest "small" request for the
217
 
             GNU C library malloc.  */
218
 
          enum { DEFAULT_MXFAST = 64 };
 
195
             zero.  This is the largest "small" request for the GNU C
 
196
             library malloc.  */
 
197
          enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
219
198
 
220
199
          n = DEFAULT_MXFAST / s;
221
200
          n += !n;