]> git.sur5r.net Git - openldap/blob - libraries/liblber/memory.c
use memchr in ber_bvchr
[openldap] / libraries / liblber / memory.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 #include "portable.h"
7
8 #include <ac/stdlib.h>
9 #include <ac/string.h>
10
11 #include "lber-int.h"
12
13 #ifdef LDAP_MEMORY_TRACE
14 # ifndef LDAP_MEMORY_DEBUG
15 #  define LDAP_MEMORY_DEBUG 1
16 # endif
17 #include <stdio.h>
18 #endif
19
20 #if LDAP_MEMORY_DEBUG
21 /*
22  * LDAP_MEMORY_DEBUG should only be enabled for the purposes of
23  * debugging memory management within OpenLDAP libraries and slapd.
24  * It should only be enabled by an experienced developer as it
25  * causes the inclusion of numerous assert()'s, many of which may
26  * be triggered by a prefectly valid program.
27  *
28  * The code behind this macro is subject to change as needed to
29  * support this testing.
30  */
31
32 struct ber_mem_hdr {
33         ber_int_t       bm_top; /* Pattern to detect buf overrun from prev buffer */
34         ber_int_t       bm_length; /* Length of user allocated area */
35 #ifdef LDAP_MEMORY_TRACE
36         ber_int_t       bm_sequence; /* Allocation sequence number */
37 #endif
38         union bmu_align_u {     /* Force alignment, pattern to detect back clobber */
39                 ber_len_t       bmu_len_t;
40                 ber_tag_t       bmu_tag_t;
41                 ber_int_t       bmu_int_t;
42
43                 size_t  bmu_size_t;
44                 void *  bmu_voidp;
45                 double  bmu_double;
46                 long    bmu_long;
47                 long    (*bmu_funcp)( double );
48                 unsigned char   bmu_char[4];
49         } ber_align;
50 #define bm_junk ber_align.bmu_len_t
51 #define bm_data ber_align.bmu_char[1]
52 #define bm_char ber_align.bmu_char
53 };
54
55 /* Pattern at top of allocated space */
56 #define LLBER_MEM_JUNK 0xdeaddadaU
57
58 static const struct ber_mem_hdr ber_int_mem_hdr = { LBER_MEM_JUNK, 0, 0 };
59
60 /* Note sequence and ber_int_options.lbu_meminuse are counters, but are not
61  * thread safe.  If you want to use these values for multithreaded applications,
62  * you must put mutexes around them, otherwise they will have incorrect values.
63  * When debugging, if you sort the debug output, the sequence number will 
64  * put allocations/frees together.  It is then a simple matter to write a script
65  * to find any allocations that don't have a buffer free function.
66  */
67 #ifdef LDAP_MEMORY_TRACE
68 static ber_int_t sequence = 0;
69 #endif
70
71 /* Pattern placed just before user data */
72 static unsigned char toppattern[4] = { 0xde, 0xad, 0xba, 0xde };
73 /* Pattern placed just after user data */
74 static unsigned char endpattern[4] = { 0xd1, 0xed, 0xde, 0xca };
75
76 #define mbu_len sizeof(ber_int_mem_hdr.ber_align)
77
78 /* Test if pattern placed just before user data is good */
79 #define testdatatop(val) ( \
80         *(val->bm_char+mbu_len-4)==toppattern[0] && \
81         *(val->bm_char+mbu_len-3)==toppattern[1] && \
82         *(val->bm_char+mbu_len-2)==toppattern[2] && \
83         *(val->bm_char+mbu_len-1)==toppattern[3] )
84
85 /* Place pattern just before user data */
86 #define setdatatop(val) *(val->bm_char+mbu_len-4)=toppattern[0]; \
87         *(val->bm_char+mbu_len-3)=toppattern[1]; \
88         *(val->bm_char+mbu_len-2)=toppattern[2]; \
89         *(val->bm_char+mbu_len-1)=toppattern[3];
90
91 /* Test if pattern placed just after user data is good */
92 #define testend(val) (  *((unsigned char *)val+0)==endpattern[0] && \
93         *((unsigned char *)val+1)==endpattern[1] && \
94         *((unsigned char *)val+2)==endpattern[2] && \
95         *((unsigned char *)val+3)==endpattern[3] )
96
97 /* Place pattern just after user data */
98 #define setend(val)     *((unsigned char *)val+0)=endpattern[0]; \
99         *((unsigned char *)val+1)=endpattern[1]; \
100         *((unsigned char *)val+2)=endpattern[2]; \
101         *((unsigned char *)val+3)=endpattern[3];
102
103 #define BER_MEM_BADADDR ((void *) &ber_int_mem_hdr.bm_data)
104 #define BER_MEM_VALID(p)        do { \
105                 assert( (p) != BER_MEM_BADADDR );       \
106                 assert( (p) != (void *) &ber_int_mem_hdr );     \
107         } while(0)
108
109 #else
110 #define BER_MEM_VALID(p)        /* no-op */
111 #endif
112
113 BerMemoryFunctions *ber_int_memory_fns = NULL;
114
115 void
116 ber_memfree( void *p )
117 {
118     ber_int_options.lbo_valid = LBER_INITIALIZED;
119
120         if( p == NULL ) {
121                 return;
122         }
123
124         BER_MEM_VALID( p );
125
126         if( ber_int_memory_fns == NULL ) {
127 #ifdef LDAP_MEMORY_DEBUG
128                 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
129                         ((char *)p - sizeof(struct ber_mem_hdr));
130                 assert( mh->bm_top == LBER_MEM_JUNK);
131                 assert( testdatatop( mh));
132                 assert( testend( (char *)&mh[1] + mh->bm_length) );
133                 ber_int_options.lbo_meminuse -= mh->bm_length;
134
135 #ifdef LDAP_MEMORY_TRACE
136                 fprintf(stderr, "0x%08x 0x%08x -f- %d ber_memfree %d\n",
137                         mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
138 #endif
139                 /* Fill the free space with poison */
140                 memset( mh, 0xff, mh->bm_length + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t));
141                 free( mh );
142 #else
143                 free( p );
144 #endif
145                 return;
146         }
147
148         assert( ber_int_memory_fns->bmf_free );
149
150         (*ber_int_memory_fns->bmf_free)( p );
151 }
152
153
154 void
155 ber_memvfree( void **vec )
156 {
157         int     i;
158
159     ber_int_options.lbo_valid = LBER_INITIALIZED;
160
161         if( vec == NULL ) {
162                 return;
163         }
164
165         BER_MEM_VALID( vec );
166
167         for ( i = 0; vec[i] != NULL; i++ ) {
168                 LBER_FREE( vec[i] );
169         }
170
171         LBER_FREE( vec );
172 }
173
174
175 void *
176 ber_memalloc( ber_len_t s )
177 {
178         void *new;
179     ber_int_options.lbo_valid = LBER_INITIALIZED;
180
181 #ifdef LDAP_MEMORY_DEBUG
182         assert( s != 0 );
183 #endif
184
185         if( s == 0 ) {
186                 return NULL;
187         }
188
189         if( ber_int_memory_fns == NULL ) {
190 #ifdef LDAP_MEMORY_DEBUG
191                 struct ber_mem_hdr *mh = malloc(s + sizeof(struct ber_mem_hdr) + sizeof( ber_int_t));
192                 if( mh == NULL ) return NULL;
193
194                 mh->bm_top = LBER_MEM_JUNK;
195                 mh->bm_length = s;
196                 setdatatop( mh);
197                 setend( (char *)&mh[1] + mh->bm_length );
198
199                 ber_int_options.lbo_meminuse += mh->bm_length;  /* Count mem inuse */
200
201 #ifdef LDAP_MEMORY_TRACE
202                 mh->bm_sequence = sequence++;
203                 fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memalloc %d\n",
204                         mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
205 #endif
206                 /* poison new memory */
207                 memset( (char *)&mh[1], 0xff, s);
208
209                 BER_MEM_VALID( &mh[1] );
210                 new = &mh[1];
211 #else
212                 new = malloc( s );
213 #endif
214         } else {
215                 new = (*ber_int_memory_fns->bmf_malloc)( s );
216         }
217
218         if( new == NULL ) {
219                 ber_errno = LBER_ERROR_MEMORY;
220         }
221
222         return new;
223 }
224
225
226 void *
227 ber_memcalloc( ber_len_t n, ber_len_t s )
228 {
229         void *new;
230     ber_int_options.lbo_valid = LBER_INITIALIZED;
231
232 #ifdef LDAP_MEMORY_DEBUG
233         assert( n != 0 && s != 0);
234 #endif
235
236         if( n == 0 || s == 0 ) {
237                 return NULL;
238         }
239
240         if( ber_int_memory_fns == NULL ) {
241 #ifdef LDAP_MEMORY_DEBUG
242                 struct ber_mem_hdr *mh = calloc(1,
243                         (n * s) + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t) );
244                 if( mh == NULL ) return NULL;
245
246                 mh->bm_top = LBER_MEM_JUNK;
247                 mh->bm_length = n*s;
248                 setdatatop( mh);
249                 setend( (char *)&mh[1] + mh->bm_length );
250
251                 ber_int_options.lbo_meminuse += mh->bm_length;
252
253 #ifdef LDAP_MEMORY_TRACE
254                 mh->bm_sequence = sequence++;
255                 fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memcalloc %d\n",
256                         mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
257 #endif
258                 BER_MEM_VALID( &mh[1] );
259                 new = &mh[1];
260 #else
261                 new = calloc( n, s );
262 #endif
263
264         } else {
265                 new = (*ber_int_memory_fns->bmf_calloc)( n, s );
266         }
267
268         if( new == NULL ) {
269                 ber_errno = LBER_ERROR_MEMORY;
270         }
271
272         return new;
273 }
274
275
276 void *
277 ber_memrealloc( void* p, ber_len_t s )
278 {
279         void *new = NULL;
280     ber_int_options.lbo_valid = LBER_INITIALIZED;
281
282         /* realloc(NULL,s) -> malloc(s) */
283         if( p == NULL ) {
284                 return LBER_MALLOC( s );
285         }
286         
287         /* realloc(p,0) -> free(p) */
288         if( s == 0 ) {
289                 LBER_FREE( p );
290                 return NULL;
291         }
292
293         BER_MEM_VALID( p );
294
295         if( ber_int_memory_fns == NULL ) {
296 #ifdef LDAP_MEMORY_DEBUG
297                 ber_int_t oldlen;
298                 struct ber_mem_hdr *mh = (struct ber_mem_hdr *)
299                         ((char *)p - sizeof(struct ber_mem_hdr));
300                 assert( mh->bm_top == LBER_MEM_JUNK);
301                 assert( testdatatop( mh));
302                 assert( testend( (char *)&mh[1] + mh->bm_length) );
303                 oldlen = mh->bm_length;
304
305                 p = realloc( mh, s + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t) );
306                 if( p == NULL ) {
307                         ber_errno = LBER_ERROR_MEMORY;
308                         return NULL;
309                 }
310
311                         mh = p;
312                 mh->bm_length = s;
313                 setend( (char *)&mh[1] + mh->bm_length );
314                 if( (s - oldlen) > 0 ) {
315                         /* poison any new memory */
316                         memset( (char *)&mh[1] + oldlen, 0xff, s - oldlen);
317                 }
318
319                 assert( mh->bm_top == LBER_MEM_JUNK);
320                 assert( testdatatop( mh));
321
322                 ber_int_options.lbo_meminuse += s - oldlen;
323 #ifdef LDAP_MEMORY_TRACE
324                 fprintf(stderr, "0x%08x 0x%08x -a- %d ber_memrealloc %d\n",
325                         mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse);
326 #endif
327                         BER_MEM_VALID( &mh[1] );
328                 return &mh[1];
329 #else
330                 new = realloc( p, s );
331 #endif
332         } else {
333                 new = (*ber_int_memory_fns->bmf_realloc)( p, s );
334         }
335
336         if( new == NULL ) {
337                 ber_errno = LBER_ERROR_MEMORY;
338         }
339
340         return new;
341 }
342
343
344 void
345 ber_bvfree( struct berval *bv )
346 {
347         ber_int_options.lbo_valid = LBER_INITIALIZED;
348
349         if( bv == NULL ) {
350                 return;
351         }
352
353         BER_MEM_VALID( bv );
354
355         if ( bv->bv_val != NULL ) {
356                 LBER_FREE( bv->bv_val );
357         }
358
359         LBER_FREE( (char *) bv );
360 }
361
362
363 void
364 ber_bvecfree( struct berval **bv )
365 {
366         int     i;
367
368         ber_int_options.lbo_valid = LBER_INITIALIZED;
369
370         if( bv == NULL ) {
371                 return;
372         }
373
374         BER_MEM_VALID( bv );
375
376         for ( i = 0; bv[i] != NULL; i++ ) {
377                 ber_bvfree( bv[i] );
378         }
379
380         LBER_FREE( (char *) bv );
381 }
382
383 int
384 ber_bvecadd( struct berval ***bvec, struct berval *bv )
385 {
386         ber_len_t i;
387         struct berval **new;
388
389         ber_int_options.lbo_valid = LBER_INITIALIZED;
390
391         if( *bvec == NULL ) {
392                 if( bv == NULL ) {
393                         /* nothing to add */
394                         return 0;
395                 }
396
397                 *bvec = LBER_MALLOC( 2 * sizeof(struct berval *) );
398
399                 if( *bvec == NULL ) {
400                         return -1;
401                 }
402
403                 (*bvec)[0] = bv;
404                 (*bvec)[1] = NULL;
405
406                 return 1;
407         }
408
409         BER_MEM_VALID( bvec );
410
411         /* count entries */
412         for ( i = 0; (*bvec)[i] != NULL; i++ ) {
413                 /* EMPTY */;
414         }
415
416         if( bv == NULL ) {
417                 return i;
418         }
419
420         new = LBER_REALLOC( *bvec, (i+2) * sizeof(struct berval *));
421
422         if( new == NULL ) {
423                 return -1;
424         }
425
426         *bvec = new;
427
428         (*bvec)[i++] = bv;
429         (*bvec)[i] = NULL;
430
431         return i;
432 }
433
434
435 struct berval *
436 ber_dupbv(
437         struct berval *dst, struct berval *src )
438 {
439         struct berval *new;
440
441         ber_int_options.lbo_valid = LBER_INITIALIZED;
442
443         if( src == NULL ) {
444                 ber_errno = LBER_ERROR_PARAM;
445                 return NULL;
446         }
447
448         if ( dst ) {
449                 new = dst;
450         } else {
451                 if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
452                         ber_errno = LBER_ERROR_MEMORY;
453                         return NULL;
454                 }
455         }
456
457         if ( src->bv_val == NULL ) {
458                 new->bv_val = NULL;
459                 new->bv_len = 0;
460                 return new;
461         }
462
463         if(( new->bv_val = LBER_MALLOC( src->bv_len + 1 )) == NULL ) {
464                 ber_errno = LBER_ERROR_MEMORY;
465                 if ( !dst )
466                         LBER_FREE( new );
467                 return NULL;
468         }
469
470         AC_MEMCPY( new->bv_val, src->bv_val, src->bv_len );
471         new->bv_val[src->bv_len] = '\0';
472         new->bv_len = src->bv_len;
473
474         return new;
475 }
476
477
478 struct berval *
479 ber_bvdup(
480         struct berval *src )
481 {
482         return ber_dupbv( NULL, src );
483 }
484
485
486 struct berval *
487 ber_str2bv(
488         LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv)
489 {
490         struct berval *new;
491
492         ber_int_options.lbo_valid = LBER_INITIALIZED;
493
494         if( s == NULL ) {
495                 ber_errno = LBER_ERROR_PARAM;
496                 return NULL;
497         }
498
499         if( bv ) {
500                 new = bv;
501         } else {
502                 if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
503                         ber_errno = LBER_ERROR_MEMORY;
504                         return NULL;
505                 }
506         }
507
508         new->bv_len = len ? len : strlen( s );
509         if ( dup ) {
510                 if ( (new->bv_val = LBER_MALLOC( new->bv_len+1 )) == NULL ) {
511                         ber_errno = LBER_ERROR_MEMORY;
512                         if ( !bv )
513                                 LBER_FREE( new );
514                         return NULL;
515                 }
516
517                 AC_MEMCPY( new->bv_val, s, new->bv_len );
518                 new->bv_val[new->bv_len] = '\0';
519         } else {
520                 new->bv_val = (char *) s;
521         }
522
523         return( new );
524 }
525
526 struct berval *
527 ber_mem2bv(
528         LDAP_CONST char *s, ber_len_t len, int dup, struct berval *bv)
529 {
530         struct berval *new;
531
532         ber_int_options.lbo_valid = LBER_INITIALIZED;
533
534         if( s == NULL || len == 0 ) {
535                 ber_errno = LBER_ERROR_PARAM;
536                 return NULL;
537         }
538
539         if( bv ) {
540                 new = bv;
541         } else {
542                 if(( new = LBER_MALLOC( sizeof(struct berval) )) == NULL ) {
543                         ber_errno = LBER_ERROR_MEMORY;
544                         return NULL;
545                 }
546         }
547
548         new->bv_len = len;
549         if ( dup ) {
550                 if ( (new->bv_val = LBER_MALLOC( new->bv_len+1 )) == NULL ) {
551                         ber_errno = LBER_ERROR_MEMORY;
552                         if ( !bv )
553                                 LBER_FREE( new );
554                         return NULL;
555                 }
556
557                 AC_MEMCPY( new->bv_val, s, new->bv_len );
558                 new->bv_val[new->bv_len] = '\0';
559         } else {
560                 new->bv_val = (char *) s;
561         }
562
563         return( new );
564 }
565
566 char *
567 ber_strdup( LDAP_CONST char *s )
568 {
569         char    *p;
570         size_t  len;
571         
572         ber_int_options.lbo_valid = LBER_INITIALIZED;
573
574 #ifdef LDAP_MEMORY_DEBUG
575         assert(s != NULL);                      /* bv damn better point to something */
576 #endif
577
578         if( s == NULL ) {
579                 ber_errno = LBER_ERROR_PARAM;
580                 return NULL;
581         }
582
583         len = strlen( s ) + 1;
584
585         if ( (p = LBER_MALLOC( len )) == NULL ) {
586                 ber_errno = LBER_ERROR_MEMORY;
587                 return NULL;
588         }
589
590         AC_MEMCPY( p, s, len );
591         return p;
592 }
593
594 char *
595 ber_strndup( LDAP_CONST char *s, ber_len_t l )
596 {
597         char    *p;
598         size_t  len;
599         
600         ber_int_options.lbo_valid = LBER_INITIALIZED;
601
602 #ifdef LDAP_MEMORY_DEBUG
603         assert(s != NULL);                      /* bv damn better point to something */
604 #endif
605
606         if( s == NULL ) {
607                 ber_errno = LBER_ERROR_PARAM;
608                 return NULL;
609         }
610
611         len = strlen( s );
612
613         if ( len > l ) {
614                 len = l;
615         }
616
617         if ( (p = LBER_MALLOC( len + 1 )) == NULL ) {
618                 ber_errno = LBER_ERROR_MEMORY;
619                 return NULL;
620         }
621
622         AC_MEMCPY( p, s, len );
623         p[ len ] = '\0';
624         return p;
625 }
626
627 char *
628 ber_strndup__( LDAP_CONST char *s, size_t l )
629 {
630         char    *p;
631         size_t  len;
632
633         if ( s == NULL ) {
634                 return NULL;
635         }
636
637         len = strlen( s );
638         if (( p = LBER_MALLOC( len + 1 ) ) == NULL ) {
639                 return NULL;
640         }
641
642         AC_MEMCPY( p, s, len );
643         p[ len ] = '\0';
644         return p;
645 }
646
647 void
648 ber_bvarray_free( BerVarray a )
649 {
650         int i;
651
652         ber_int_options.lbo_valid = LBER_INITIALIZED;
653
654         if (a) {
655                 BER_MEM_VALID( a );
656
657                 for (i=0; a[i].bv_val; i++) {
658                         LBER_FREE(a[i].bv_val);
659                 }
660
661                 LBER_FREE(a);
662         }
663 }
664
665 int
666 ber_bvarray_add( BerVarray *a, BerValue *bv )
667 {
668         int     n;
669
670         ber_int_options.lbo_valid = LBER_INITIALIZED;
671
672         if ( *a == NULL ) {
673                 if (bv == NULL) {
674                         return 0;
675                 }
676                 n = 0;
677                 *a = (BerValue *) LBER_MALLOC( 2 * sizeof(BerValue) );
678         } else {
679                 BER_MEM_VALID( a );
680
681                 for ( n = 0; *a != NULL && (*a)[n].bv_val != NULL; n++ ) {
682                         ;       /* NULL */
683                 }
684
685                 if (bv == NULL) {
686                         return n;
687                 }
688                 *a = (BerValue *) LBER_REALLOC( (char *) *a,
689                     (n + 2) * sizeof(BerValue) );
690         }
691         if ( *a == NULL ) {
692                 return -1;
693         }
694
695         (*a)[n++] = *bv;
696         (*a)[n].bv_val = NULL;
697
698         return n;
699 }
700