~ubuntu-branches/ubuntu/raring/flac/raring

« back to all changes in this revision

Viewing changes to src/libFLAC/memory.c

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2007-12-06 16:57:20 UTC
  • mto: (8.1.1 lenny)
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: james.westby@ubuntu.com-20071206165720-4przr8grn6ha3e3a
Tags: upstream-1.2.1
ImportĀ upstreamĀ versionĀ 1.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
 
36
36
#include "private/memory.h"
37
37
#include "FLAC/assert.h"
 
38
#include "share/alloc.h"
38
39
 
39
40
void *FLAC__memory_alloc_aligned(size_t bytes, void **aligned_address)
40
41
{
44
45
 
45
46
#ifdef FLAC__ALIGN_MALLOC_DATA
46
47
        /* align on 32-byte (256-bit) boundary */
47
 
        x = malloc(bytes+31);
 
48
        x = safe_malloc_add_2op_(bytes, /*+*/31);
 
49
#ifdef SIZEOF_VOIDP
 
50
#if SIZEOF_VOIDP == 4
 
51
                /* could do  *aligned_address = x + ((unsigned) (32 - (((unsigned)x) & 31))) & 31; */
 
52
                *aligned_address = (void*)(((unsigned)x + 31) & -32);
 
53
#elif SIZEOF_VOIDP == 8
 
54
                *aligned_address = (void*)(((FLAC__uint64)x + 31) & (FLAC__uint64)(-((FLAC__int64)32)));
 
55
#else
 
56
# error  Unsupported sizeof(void*)
 
57
#endif
 
58
#else
48
59
        /* there's got to be a better way to do this right for all archs */
49
60
        if(sizeof(void*) == sizeof(unsigned))
50
61
                *aligned_address = (void*)(((unsigned)x + 31) & -32);
52
63
                *aligned_address = (void*)(((FLAC__uint64)x + 31) & (FLAC__uint64)(-((FLAC__int64)32)));
53
64
        else
54
65
                return 0;
 
66
#endif
55
67
#else
56
 
        x = malloc(bytes);
 
68
        x = safe_malloc_(bytes);
57
69
        *aligned_address = x;
58
70
#endif
59
71
        return x;
72
84
        FLAC__ASSERT(0 != aligned_pointer);
73
85
        FLAC__ASSERT(unaligned_pointer != aligned_pointer);
74
86
 
75
 
        pu = (FLAC__int32*)FLAC__memory_alloc_aligned(sizeof(FLAC__int32) * elements, &u.pv);
 
87
        if((size_t)elements > SIZE_MAX / sizeof(*pu)) /* overflow check */
 
88
                return false;
 
89
 
 
90
        pu = (FLAC__int32*)FLAC__memory_alloc_aligned(sizeof(*pu) * (size_t)elements, &u.pv);
76
91
        if(0 == pu) {
77
92
                return false;
78
93
        }
98
113
        FLAC__ASSERT(0 != aligned_pointer);
99
114
        FLAC__ASSERT(unaligned_pointer != aligned_pointer);
100
115
 
101
 
        pu = (FLAC__uint32*)FLAC__memory_alloc_aligned(sizeof(FLAC__uint32) * elements, &u.pv);
 
116
        if((size_t)elements > SIZE_MAX / sizeof(*pu)) /* overflow check */
 
117
                return false;
 
118
 
 
119
        pu = (FLAC__uint32*)FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv);
102
120
        if(0 == pu) {
103
121
                return false;
104
122
        }
124
142
        FLAC__ASSERT(0 != aligned_pointer);
125
143
        FLAC__ASSERT(unaligned_pointer != aligned_pointer);
126
144
 
127
 
        pu = (FLAC__uint64*)FLAC__memory_alloc_aligned(sizeof(FLAC__uint64) * elements, &u.pv);
 
145
        if((size_t)elements > SIZE_MAX / sizeof(*pu)) /* overflow check */
 
146
                return false;
 
147
 
 
148
        pu = (FLAC__uint64*)FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv);
128
149
        if(0 == pu) {
129
150
                return false;
130
151
        }
150
171
        FLAC__ASSERT(0 != aligned_pointer);
151
172
        FLAC__ASSERT(unaligned_pointer != aligned_pointer);
152
173
 
153
 
        pu = (unsigned*)FLAC__memory_alloc_aligned(sizeof(unsigned) * elements, &u.pv);
 
174
        if((size_t)elements > SIZE_MAX / sizeof(*pu)) /* overflow check */
 
175
                return false;
 
176
 
 
177
        pu = (unsigned*)FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv);
154
178
        if(0 == pu) {
155
179
                return false;
156
180
        }
178
202
        FLAC__ASSERT(0 != aligned_pointer);
179
203
        FLAC__ASSERT(unaligned_pointer != aligned_pointer);
180
204
 
181
 
        pu = (FLAC__real*)FLAC__memory_alloc_aligned(sizeof(FLAC__real) * elements, &u.pv);
 
205
        if((size_t)elements > SIZE_MAX / sizeof(*pu)) /* overflow check */
 
206
                return false;
 
207
 
 
208
        pu = (FLAC__real*)FLAC__memory_alloc_aligned(sizeof(*pu) * elements, &u.pv);
182
209
        if(0 == pu) {
183
210
                return false;
184
211
        }