~ubuntu-branches/ubuntu/gutsy/vnc4/gutsy

« back to all changes in this revision

Viewing changes to unix/xc/lib/Xt/Alloc.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2006-05-15 20:35:17 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060515203517-l4lre1ku942mn26k
Tags: 4.1.1+X4.3.0-10
* Correction of critical security issue. Thanks to Martin Kogler
  <e9925248@student.tuwien.ac.at> that informed me about the issue,
  and provided the patch.
  This flaw was originally found by Steve Wiseman of intelliadmin.com.
* Applied patch from Javier Kohen <jkohen@users.sourceforge.net> that
  inform the user that only 8 first characters of the password will
  actually be used when typing more than 8 characters, closes:
  #355619.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Xorg: Alloc.c,v 1.4 2001/02/09 02:03:53 xorgcvs Exp $ */
 
2
 
 
3
/***********************************************************
 
4
Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
 
5
Copyright 1993 by Sun Microsystems, Inc. Mountain View, CA.
 
6
 
 
7
                        All Rights Reserved
 
8
 
 
9
Permission to use, copy, modify, and distribute this software and its 
 
10
documentation for any purpose and without fee is hereby granted, 
 
11
provided that the above copyright notice appear in all copies and that
 
12
both that copyright notice and this permission notice appear in 
 
13
supporting documentation, and that the names of Digital or Sun not be
 
14
used in advertising or publicity pertaining to distribution of the
 
15
software without specific, written prior permission.  
 
16
 
 
17
DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 
18
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
 
19
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
 
20
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 
21
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
 
22
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 
23
SOFTWARE.
 
24
 
 
25
SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
 
26
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
 
27
NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
 
28
ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
 
29
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
 
30
PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
 
31
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
 
32
THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
33
 
 
34
******************************************************************/
 
35
 
 
36
/*
 
37
 
 
38
Copyright 1987, 1988, 1998  The Open Group
 
39
 
 
40
Permission to use, copy, modify, distribute, and sell this software and its
 
41
documentation for any purpose is hereby granted without fee, provided that
 
42
the above copyright notice appear in all copies and that both that
 
43
copyright notice and this permission notice appear in supporting
 
44
documentation.
 
45
 
 
46
The above copyright notice and this permission notice shall be included in
 
47
all copies or substantial portions of the Software.
 
48
 
 
49
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
50
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
51
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 
52
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 
53
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
54
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
55
 
 
56
Except as contained in this notice, the name of The Open Group shall not be
 
57
used in advertising or otherwise to promote the sale, use or other dealings
 
58
in this Software without prior written authorization from The Open Group.
 
59
 
 
60
*/
 
61
/* $XFree86: xc/lib/Xt/Alloc.c,v 1.9 2001/12/14 19:56:07 dawes Exp $ */
 
62
 
 
63
/*
 
64
 * X Toolkit Memory Allocation Routines
 
65
 *
 
66
 * Uses Xlib memory management, which is spec'd to be re-entrant.
 
67
 */
 
68
 
 
69
#include "IntrinsicI.h"
 
70
#include "InitialI.h"
 
71
#undef _XBCOPYFUNC
 
72
 
 
73
#include <stdlib.h>
 
74
 
 
75
#define Xmalloc(size) malloc((size))
 
76
#define Xrealloc(ptr, size) realloc((ptr), (size))
 
77
#define Xcalloc(nelem, elsize) calloc((nelem), (elsize))
 
78
#define Xfree(ptr) free(ptr)
 
79
 
 
80
#ifdef _XNEEDBCOPYFUNC
 
81
void _XtBcopy(src, dst, length)
 
82
    char *src, *dst;
 
83
    int length;
 
84
{
 
85
    if (src < dst) {
 
86
        dst += length;
 
87
        src += length;
 
88
        while (length--)
 
89
            *--dst = *--src;
 
90
    } else {
 
91
        while (length--)
 
92
            *dst++ = *src++;
 
93
    }
 
94
}
 
95
#endif
 
96
 
 
97
void _XtAllocError(type)
 
98
    String type;
 
99
{
 
100
    Cardinal num_params = 1;
 
101
    if (type == NULL) type = "local memory allocation";
 
102
    XtErrorMsg("allocError", type, XtCXtToolkitError,
 
103
               "Cannot perform %s", &type, &num_params);
 
104
}
 
105
 
 
106
void _XtHeapInit(heap)
 
107
    Heap*       heap;
 
108
{
 
109
    heap->start = NULL;
 
110
    heap->bytes_remaining = 0;
 
111
}
 
112
 
 
113
#ifndef XTTRACEMEMORY
 
114
 
 
115
char *XtMalloc(size)
 
116
    unsigned size;
 
117
{
 
118
    char *ptr;
 
119
 
 
120
#if defined (MALLOC_0_RETURNS_NULL) && defined(XTMALLOC_BC)
 
121
    /* preserve this (broken) behavior until everyone fixes their apps */
 
122
    if (!size) size = 1;
 
123
#endif
 
124
    if ((ptr = Xmalloc(size)) == NULL)
 
125
        _XtAllocError("malloc");
 
126
 
 
127
    return(ptr);
 
128
}
 
129
 
 
130
char *XtRealloc(ptr, size)
 
131
    char     *ptr;
 
132
    unsigned size;
 
133
{
 
134
   if (ptr == NULL) {
 
135
#if MALLOC_0_RETURNS_NULL
 
136
        if (!size) size = 1;
 
137
#endif
 
138
        return(XtMalloc(size));
 
139
   } else if ((ptr = Xrealloc(ptr, size)) == NULL
 
140
#if MALLOC_0_RETURNS_NULL
 
141
                && size
 
142
#endif
 
143
        )
 
144
        _XtAllocError("realloc");
 
145
 
 
146
   return(ptr);
 
147
}
 
148
 
 
149
char *XtCalloc(num, size)
 
150
    unsigned num, size;
 
151
{
 
152
    char *ptr;
 
153
 
 
154
#if defined (MALLOC_0_RETURNS_NULL) && defined(XTMALLOC_BC)
 
155
    /* preserve this (broken) behavior until everyone fixes their apps */
 
156
    if (!size) num = size = 1;
 
157
#endif
 
158
    if ((ptr = Xcalloc(num, size)) == NULL)
 
159
        _XtAllocError("calloc");
 
160
 
 
161
    return(ptr);
 
162
}
 
163
 
 
164
void XtFree(ptr)
 
165
    char *ptr;
 
166
{
 
167
    if (ptr != NULL) Xfree(ptr);
 
168
}
 
169
 
 
170
char* __XtMalloc(size)
 
171
    unsigned size;
 
172
{
 
173
#ifdef MALLOC_0_RETURNS_NULL
 
174
    if (!size) size = 1;
 
175
#endif
 
176
    return XtMalloc (size);
 
177
}
 
178
 
 
179
char* __XtCalloc(num, size)
 
180
    unsigned num, size;
 
181
{
 
182
#ifdef MALLOC_0_RETURNS_NULL
 
183
    if (!size) num = size = 1;
 
184
#endif
 
185
    return XtCalloc(num, size);
 
186
}
 
187
 
 
188
#ifndef HEAP_SEGMENT_SIZE
 
189
#define HEAP_SEGMENT_SIZE 1492
 
190
#endif
 
191
 
 
192
char* _XtHeapAlloc(heap, bytes)
 
193
    Heap*       heap;
 
194
    Cardinal    bytes;
 
195
{
 
196
    register char* heap_loc;
 
197
    if (heap == NULL) return XtMalloc(bytes);
 
198
    if (heap->bytes_remaining < (int)bytes) {
 
199
        if ((bytes + sizeof(char*)) >= (HEAP_SEGMENT_SIZE>>1)) {
 
200
            /* preserve current segment; insert this one in front */
 
201
#ifdef _TRACE_HEAP
 
202
            printf( "allocating large segment (%d bytes) on heap %#x\n",
 
203
                    bytes, heap );
 
204
#endif
 
205
            heap_loc = XtMalloc(bytes + sizeof(char*));
 
206
            if (heap->start) {
 
207
                *(char**)heap_loc = *(char**)heap->start;
 
208
                *(char**)heap->start = heap_loc;
 
209
            }
 
210
            else {
 
211
                *(char**)heap_loc = NULL;
 
212
                heap->start = heap_loc;
 
213
            }
 
214
            return heap_loc + sizeof(char*);
 
215
        }
 
216
        /* else discard remainder of this segment */
 
217
#ifdef _TRACE_HEAP
 
218
        printf( "allocating new segment on heap %#x\n", heap );
 
219
#endif
 
220
        heap_loc = XtMalloc((unsigned)HEAP_SEGMENT_SIZE);
 
221
        *(char**)heap_loc = heap->start;
 
222
        heap->start = heap_loc;
 
223
        heap->current = heap_loc + sizeof(char*);
 
224
        heap->bytes_remaining = HEAP_SEGMENT_SIZE - sizeof(char*);
 
225
    }
 
226
    bytes = (bytes + (sizeof(long) - 1)) & (~(sizeof(long) - 1));
 
227
    heap_loc = heap->current;
 
228
    heap->current += bytes;
 
229
    heap->bytes_remaining -= bytes; /* can be negative, if rounded */
 
230
    return heap_loc;
 
231
}
 
232
 
 
233
void _XtHeapFree(heap)
 
234
    Heap*       heap;
 
235
{
 
236
    char* segment = heap->start;
 
237
    while (segment != NULL) {
 
238
        char* next_segment = *(char**)segment;
 
239
        XtFree(segment);
 
240
        segment = next_segment;
 
241
    }
 
242
    heap->start = NULL;
 
243
    heap->bytes_remaining = 0;
 
244
}
 
245
 
 
246
#else
 
247
 
 
248
/*
 
249
 * X Toolkit Memory Trace Allocation Routines
 
250
 */
 
251
 
 
252
#undef XtMalloc
 
253
#undef XtRealloc
 
254
#undef XtCalloc
 
255
#undef XtFree
 
256
 
 
257
typedef struct _Stats *StatsPtr;
 
258
typedef struct _Stats {
 
259
    StatsPtr prev, next;
 
260
    char *file;
 
261
    int line;
 
262
    unsigned size;
 
263
    unsigned long seq;
 
264
    XtPointer heap;
 
265
} Stats;
 
266
 
 
267
static StatsPtr XtMemory = (StatsPtr)NULL;
 
268
static unsigned long ActiveXtMemory = 0;
 
269
static unsigned long XtSeqId = 0;
 
270
static unsigned long XtSeqBreakpoint = ~0;
 
271
 
 
272
#define StatsSize(n) ((((n) + (sizeof(long) - 1)) & ~(sizeof(long) - 1)) + sizeof(Stats))
 
273
#define ToStats(ptr) ((StatsPtr)(ptr - sizeof(Stats)))
 
274
#define ToMem(ptr) (((char *)ptr) + sizeof(Stats))
 
275
 
 
276
#define CHAIN(ptr,len,hp) \
 
277
    ptr->next = XtMemory; \
 
278
    if (XtMemory) \
 
279
        XtMemory->prev = ptr; \
 
280
    XtMemory = ptr; \
 
281
    ptr->prev = (StatsPtr)NULL; \
 
282
    ptr->file = file; \
 
283
    ptr->line = line; \
 
284
    ptr->size = len; \
 
285
    ptr->heap = hp; \
 
286
    if (file) \
 
287
        ActiveXtMemory += len; \
 
288
    ptr->seq = XtSeqId; \
 
289
    if (XtSeqId == XtSeqBreakpoint) \
 
290
        _XtBreakpoint(ptr); \
 
291
    XtSeqId++
 
292
 
 
293
/*ARGUSED*/
 
294
static void _XtBreakpoint(mem)
 
295
    StatsPtr mem;
 
296
{
 
297
    mem->seq = XtSeqId; /* avoid being optimized out of existence */
 
298
}
 
299
 
 
300
char *_XtMalloc(size, file, line)
 
301
    unsigned size;
 
302
    char *file;
 
303
    int line;
 
304
{
 
305
    StatsPtr ptr;
 
306
    unsigned newsize;
 
307
    char* retval = NULL;
 
308
 
 
309
    LOCK_PROCESS;
 
310
    newsize = StatsSize(size);
 
311
    if ((ptr = (StatsPtr)Xmalloc(newsize)) == NULL)
 
312
        _XtAllocError("malloc");
 
313
    CHAIN(ptr, size, NULL);
 
314
    retval = (ToMem(ptr));
 
315
    UNLOCK_PROCESS;
 
316
    return retval;
 
317
}
 
318
 
 
319
char *XtMalloc(size)
 
320
    unsigned size;
 
321
{
 
322
    return _XtMalloc(size, (char *)NULL, 0);
 
323
}
 
324
 
 
325
char *_XtRealloc(ptr, size, file, line)
 
326
    char     *ptr;
 
327
    unsigned size;
 
328
    char *file;
 
329
    int line;
 
330
{
 
331
   char *newptr;
 
332
 
 
333
   LOCK_PROCESS;
 
334
   newptr = _XtMalloc(size, file, line);
 
335
   if (ptr) {
 
336
       unsigned copysize = ToStats(ptr)->size;
 
337
       if (copysize > size) copysize = size;
 
338
       memmove(newptr, ptr, copysize);
 
339
       _XtFree(ptr);
 
340
   }
 
341
   UNLOCK_PROCESS;
 
342
   return(newptr);
 
343
}
 
344
 
 
345
char *XtRealloc(ptr, size)
 
346
    char     *ptr;
 
347
    unsigned size;
 
348
{
 
349
    return _XtRealloc(ptr, size, (char *)NULL, 0);
 
350
}
 
351
 
 
352
char *_XtCalloc(num, size, file, line)
 
353
    unsigned num, size;
 
354
    char *file;
 
355
    int line;
 
356
{
 
357
    StatsPtr ptr;
 
358
    unsigned total, newsize;
 
359
    char* retval = NULL;
 
360
 
 
361
    LOCK_PROCESS;
 
362
    total = num * size;
 
363
    newsize = StatsSize(total);
 
364
    if ((ptr = (StatsPtr)Xcalloc(newsize, 1)) == NULL)
 
365
        _XtAllocError("calloc");
 
366
    CHAIN(ptr, total, NULL);
 
367
    retval = (ToMem(ptr));
 
368
    UNLOCK_PROCESS;
 
369
    return retval;
 
370
}
 
371
 
 
372
char *XtCalloc(num, size)
 
373
    unsigned num, size;
 
374
{
 
375
    return _XtCalloc(num, size, (char *)NULL, 0);
 
376
}
 
377
 
 
378
Boolean _XtIsValidPointer(ptr)
 
379
    char *ptr;
 
380
{
 
381
    register StatsPtr mem;
 
382
    register StatsPtr stp = ToStats(ptr);
 
383
 
 
384
    LOCK_PROCESS;
 
385
    for (mem = XtMemory; mem; mem = mem->next) {
 
386
        if (mem == stp) {
 
387
            UNLOCK_PROCESS;
 
388
            return True;
 
389
        }
 
390
    }
 
391
    UNLOCK_PROCESS;
 
392
    return False;
 
393
}
 
394
 
 
395
Boolean _XtValidateMemory = False;
 
396
 
 
397
void _XtFree(ptr)
 
398
    char *ptr;
 
399
{
 
400
   register StatsPtr stp;
 
401
 
 
402
   LOCK_PROCESS;
 
403
   if (ptr) {
 
404
       if (_XtValidateMemory && !_XtIsValidPointer(ptr))
 
405
           abort();
 
406
       stp = ToStats(ptr);
 
407
       if (stp->file)
 
408
           ActiveXtMemory -= stp->size;
 
409
       if (stp->prev)
 
410
           stp->prev->next = stp->next;
 
411
       else
 
412
           XtMemory = stp->next;
 
413
       if (stp->next)
 
414
           stp->next->prev = stp->prev;
 
415
       Xfree((char *)stp);
 
416
   }
 
417
   UNLOCK_PROCESS;
 
418
}
 
419
 
 
420
void XtFree(ptr)
 
421
    char *ptr;
 
422
{
 
423
   _XtFree(ptr);
 
424
}
 
425
 
 
426
char *_XtHeapMalloc(heap, size, file, line)
 
427
    Heap *heap;
 
428
    Cardinal size;
 
429
    char *file;
 
430
    int line;
 
431
{
 
432
    StatsPtr ptr;
 
433
    unsigned newsize;
 
434
    XtPointer hp = (XtPointer) heap;
 
435
    char* retval = NULL;
 
436
 
 
437
    LOCK_PROCESS;
 
438
    newsize = StatsSize(size);
 
439
    if ((ptr = (StatsPtr)Xmalloc(newsize)) == NULL)
 
440
        _XtAllocError("malloc");
 
441
    CHAIN(ptr, size, hp);
 
442
    retval = (ToMem(ptr));
 
443
    UNLOCK_PROCESS;
 
444
    return retval;
 
445
}
 
446
 
 
447
void _XtHeapFree(heap)
 
448
    register XtPointer heap;
 
449
{
 
450
    register StatsPtr mem, next;
 
451
 
 
452
    LOCK_PROCESS;
 
453
    for (mem = XtMemory; mem; mem = next) {
 
454
        next = mem->next;
 
455
        if (mem->heap == heap) {
 
456
            if (mem->file)
 
457
                ActiveXtMemory -= mem->size;
 
458
            if (mem->prev)
 
459
                mem->prev->next = next;
 
460
            else
 
461
                XtMemory = next;
 
462
            if (next)
 
463
                next->prev = mem->prev;
 
464
            Xfree((char *)mem);
 
465
        }
 
466
    }
 
467
    UNLOCK_PROCESS;
 
468
}
 
469
 
 
470
#include <stdio.h>
 
471
 
 
472
void _XtPrintMemory(filename)
 
473
char * filename;
 
474
{
 
475
    register StatsPtr mem;
 
476
    FILE *f;
 
477
 
 
478
    if (filename == NULL)
 
479
        f = stderr;
 
480
    else 
 
481
        f = fopen(filename, "w");
 
482
    LOCK_PROCESS;
 
483
    fprintf(f, "total size: %d\n", ActiveXtMemory);
 
484
    for (mem = XtMemory; mem; mem = mem->next) {
 
485
        if (mem->file)
 
486
            fprintf(f, "size: %6d  seq: %5d  %12s(%4d)  %s\n",
 
487
                    mem->size, mem->seq,
 
488
                    mem->file, mem->line, mem->heap ? "heap" : "");
 
489
    }
 
490
    UNLOCK_PROCESS;
 
491
    if (filename) fclose(f);
 
492
}
 
493
 
 
494
#endif  /* XTTRACEMEMORY */