~ubuntu-branches/ubuntu/precise/wget/precise-proposed

« back to all changes in this revision

Viewing changes to lib/xsize.h

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2011-10-19 00:00:09 UTC
  • mfrom: (2.1.13 sid)
  • Revision ID: james.westby@ubuntu.com-20111019000009-8p33w3wz4b1rdri0
Tags: 1.13-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Add wget-udeb to ship wget.gnu as alternative to busybox wget
    implementation.
  - Depend on libssl-dev 0.9.8k-7ubuntu4 (LP: #503339)
* Dropped changes, superseded in Debian:
  - Keep build dependencies in main:
    + debian/control: remove info2man build-dep
    + debian/patches/series: disable wget-infopod_generated_manpage
  - Mark wget Multi-Arch: foreign, so packages that aren't of the same arch
    can depend on it.
* Pass --with-ssl=openssl; we don't want to use gnutls, there's no udeb for
  it.
* Add a second build pass for the udeb, so we can build without libidn.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- buffer-read-only: t -*- vi: set ro: */
 
2
/* DO NOT EDIT! GENERATED AUTOMATICALLY! */
 
3
/* xsize.h -- Checked size_t computations.
 
4
 
 
5
   Copyright (C) 2003, 2008-2011 Free Software Foundation, Inc.
 
6
 
 
7
   This program is free software; you can redistribute it and/or modify
 
8
   it under the terms of the GNU General Public License as published by
 
9
   the Free Software Foundation; either version 3, or (at your option)
 
10
   any later version.
 
11
 
 
12
   This program is distributed in the hope that it will be useful,
 
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
   GNU General Public License for more details.
 
16
 
 
17
   You should have received a copy of the GNU General Public License
 
18
   along with this program; if not, write to the Free Software Foundation,
 
19
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
20
 
 
21
#ifndef _XSIZE_H
 
22
#define _XSIZE_H
 
23
 
 
24
/* Get size_t.  */
 
25
#include <stddef.h>
 
26
 
 
27
/* Get SIZE_MAX.  */
 
28
#include <limits.h>
 
29
#if HAVE_STDINT_H
 
30
# include <stdint.h>
 
31
#endif
 
32
 
 
33
/* The size of memory objects is often computed through expressions of
 
34
   type size_t. Example:
 
35
      void* p = malloc (header_size + n * element_size).
 
36
   These computations can lead to overflow.  When this happens, malloc()
 
37
   returns a piece of memory that is way too small, and the program then
 
38
   crashes while attempting to fill the memory.
 
39
   To avoid this, the functions and macros in this file check for overflow.
 
40
   The convention is that SIZE_MAX represents overflow.
 
41
   malloc (SIZE_MAX) is not guaranteed to fail -- think of a malloc
 
42
   implementation that uses mmap --, it's recommended to use size_overflow_p()
 
43
   or size_in_bounds_p() before invoking malloc().
 
44
   The example thus becomes:
 
45
      size_t size = xsum (header_size, xtimes (n, element_size));
 
46
      void *p = (size_in_bounds_p (size) ? malloc (size) : NULL);
 
47
*/
 
48
 
 
49
/* Convert an arbitrary value >= 0 to type size_t.  */
 
50
#define xcast_size_t(N) \
 
51
  ((N) <= SIZE_MAX ? (size_t) (N) : SIZE_MAX)
 
52
 
 
53
/* Sum of two sizes, with overflow check.  */
 
54
static inline size_t
 
55
#if __GNUC__ >= 3
 
56
__attribute__ ((__pure__))
 
57
#endif
 
58
xsum (size_t size1, size_t size2)
 
59
{
 
60
  size_t sum = size1 + size2;
 
61
  return (sum >= size1 ? sum : SIZE_MAX);
 
62
}
 
63
 
 
64
/* Sum of three sizes, with overflow check.  */
 
65
static inline size_t
 
66
#if __GNUC__ >= 3
 
67
__attribute__ ((__pure__))
 
68
#endif
 
69
xsum3 (size_t size1, size_t size2, size_t size3)
 
70
{
 
71
  return xsum (xsum (size1, size2), size3);
 
72
}
 
73
 
 
74
/* Sum of four sizes, with overflow check.  */
 
75
static inline size_t
 
76
#if __GNUC__ >= 3
 
77
__attribute__ ((__pure__))
 
78
#endif
 
79
xsum4 (size_t size1, size_t size2, size_t size3, size_t size4)
 
80
{
 
81
  return xsum (xsum (xsum (size1, size2), size3), size4);
 
82
}
 
83
 
 
84
/* Maximum of two sizes, with overflow check.  */
 
85
static inline size_t
 
86
#if __GNUC__ >= 3
 
87
__attribute__ ((__pure__))
 
88
#endif
 
89
xmax (size_t size1, size_t size2)
 
90
{
 
91
  /* No explicit check is needed here, because for any n:
 
92
     max (SIZE_MAX, n) == SIZE_MAX and max (n, SIZE_MAX) == SIZE_MAX.  */
 
93
  return (size1 >= size2 ? size1 : size2);
 
94
}
 
95
 
 
96
/* Multiplication of a count with an element size, with overflow check.
 
97
   The count must be >= 0 and the element size must be > 0.
 
98
   This is a macro, not an inline function, so that it works correctly even
 
99
   when N is of a wider type and N > SIZE_MAX.  */
 
100
#define xtimes(N, ELSIZE) \
 
101
  ((N) <= SIZE_MAX / (ELSIZE) ? (size_t) (N) * (ELSIZE) : SIZE_MAX)
 
102
 
 
103
/* Check for overflow.  */
 
104
#define size_overflow_p(SIZE) \
 
105
  ((SIZE) == SIZE_MAX)
 
106
/* Check against overflow.  */
 
107
#define size_in_bounds_p(SIZE) \
 
108
  ((SIZE) != SIZE_MAX)
 
109
 
 
110
#endif /* _XSIZE_H */