diff options
author | Andrew Tridgell <tridge@samba.org> | 2005-01-07 04:39:16 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 13:08:30 -0500 |
commit | 11ce2cfd70df264c5c91b4daaa9a01c5abc673b0 (patch) | |
tree | f00bf95389509ca8655f3b1cf1dfec76289790b3 /source4/lib | |
parent | 066134f2414e8688fd980b619f9e597538c8766d (diff) | |
download | samba-11ce2cfd70df264c5c91b4daaa9a01c5abc673b0.tar.gz samba-11ce2cfd70df264c5c91b4daaa9a01c5abc673b0.tar.bz2 samba-11ce2cfd70df264c5c91b4daaa9a01c5abc673b0.zip |
r4591: - converted the other _p talloc functions to not need _p
- added #if TALLOC_DEPRECATED around the _p functions
- fixes the code that broke from the above
while doing this I fixed quite a number of places that were
incorrectly using the non type-safe talloc functions to use the type
safe ones. Some were even doing multiplies for array allocation, which
is potentially unsafe.
(This used to be commit 6e7754abd0c225527fb38363996a6e241b87b37e)
Diffstat (limited to 'source4/lib')
-rw-r--r-- | source4/lib/charcnv.c | 2 | ||||
-rw-r--r-- | source4/lib/registry/common/reg_util.c | 2 | ||||
-rw-r--r-- | source4/lib/talloc/talloc.c | 14 | ||||
-rw-r--r-- | source4/lib/talloc/talloc.h | 37 | ||||
-rw-r--r-- | source4/lib/talloc/talloc_guide.txt | 49 | ||||
-rw-r--r-- | source4/lib/talloc/testsuite.c | 20 |
6 files changed, 72 insertions, 52 deletions
diff --git a/source4/lib/charcnv.c b/source4/lib/charcnv.c index 7e9750e948..d5727feb2c 100644 --- a/source4/lib/charcnv.c +++ b/source4/lib/charcnv.c @@ -229,7 +229,7 @@ ssize_t convert_string_talloc(TALLOC_CTX *ctx, charset_t from, charset_t to, outbuf = NULL; convert: destlen = 2 + (destlen*3); - ob = (char *)talloc_realloc(ctx, outbuf, destlen); + ob = talloc_realloc(ctx, outbuf, char, destlen); if (!ob) { DEBUG(0, ("convert_string_talloc: realloc failed!\n")); talloc_free(outbuf); diff --git a/source4/lib/registry/common/reg_util.c b/source4/lib/registry/common/reg_util.c index 67c62fe5c8..ef46dd6174 100644 --- a/source4/lib/registry/common/reg_util.c +++ b/source4/lib/registry/common/reg_util.c @@ -63,7 +63,7 @@ char *reg_val_data_string(TALLOC_CTX *mem_ctx, struct registry_value *v) return ret; case REG_BINARY: - ret = talloc_array(mem_ctx, 3, v->data_len+1, "REG_BINARY"); + ret = talloc_array_size(mem_ctx, 3, v->data_len+1); asciip = ret; for (i=0; i<v->data_len; i++) { int str_rem = v->data_len * 3 - (asciip - ret); diff --git a/source4/lib/talloc/talloc.c b/source4/lib/talloc/talloc.c index bcadf40cfb..6d11cb00e9 100644 --- a/source4/lib/talloc/talloc.c +++ b/source4/lib/talloc/talloc.c @@ -932,7 +932,7 @@ static char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) } len = vsnprintf(NULL, 0, fmt, ap2); - s = talloc_realloc(NULL, s, s_len + len+1); + s = talloc_realloc(NULL, s, char, s_len + len+1); if (!s) return NULL; VA_COPY(ap2, ap); @@ -961,7 +961,7 @@ char *talloc_asprintf_append(char *s, const char *fmt, ...) /* alloc an array, checking for integer overflow in the array size */ -void *talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name) +void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name) { if (count >= MAX_TALLOC_SIZE/el_size) { return NULL; @@ -972,7 +972,7 @@ void *talloc_array(const void *ctx, size_t el_size, unsigned count, const char * /* alloc an zero array, checking for integer overflow in the array size */ -void *talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name) +void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name) { if (count >= MAX_TALLOC_SIZE/el_size) { return NULL; @@ -984,16 +984,12 @@ void *talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const c /* realloc an array, checking for integer overflow in the array size */ -void *talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name) +void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name) { if (count >= MAX_TALLOC_SIZE/el_size) { return NULL; } - ptr = talloc_realloc(ctx, ptr, el_size * count); - if (ptr) { - talloc_set_name_const(ptr, name); - } - return ptr; + return _talloc_realloc(ctx, ptr, el_size * count, name); } /* diff --git a/source4/lib/talloc/talloc.h b/source4/lib/talloc/talloc.h index 747b1c6ba6..8169782441 100644 --- a/source4/lib/talloc/talloc.h +++ b/source4/lib/talloc/talloc.h @@ -32,17 +32,26 @@ typedef void TALLOC_CTX; #define __LINESTR__ _STRING_LINE2_(__LINE__) #define __location__ __FILE__ ":" __LINESTR__ +#ifndef TALLOC_DEPRECATED +#define TALLOC_DEPRECATED 0 +#endif + /* useful macros for creating type checked pointers */ #define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type) -#define talloc_p(ctx, type) talloc(ctx, type) #define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__) -#define talloc_zero(ctx, size) _talloc_zero(ctx, size, __location__) -#define talloc_realloc(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__) + #define talloc_new(ctx) talloc_named_const(ctx, 0, "talloc_new: " __location__) -#define talloc_zero_p(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type) -#define talloc_zero_array_p(ctx, type, count) (type *)talloc_zero_array(ctx, sizeof(type), count, __location__) -#define talloc_array_p(ctx, type, count) (type *)talloc_array(ctx, sizeof(type), count, __location__) -#define talloc_realloc_p(ctx, p, type, count) (type *)talloc_realloc_array(ctx, p, sizeof(type), count, __location__) + +#define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type) +#define talloc_zero_size(ctx, size) _talloc_zero(ctx, size, __location__) + +#define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, __location__) +#define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, __location__) +#define talloc_array_size(ctx, size, count) _talloc_array(ctx, size, count, __location__) + +#define talloc_realloc(ctx, p, type, count) (type *)_talloc_realloc_array(ctx, p, sizeof(type), count, __location__) +#define talloc_realloc_size(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__) + #define talloc_memdup(t, p, size) _talloc_memdup(t, p, size, __location__) #define talloc_destroy(ctx) talloc_free(ctx) @@ -54,6 +63,14 @@ typedef void TALLOC_CTX; #define data_blob(ptr, size) data_blob_named(ptr, size, "DATA_BLOB: "__location__) #define data_blob_talloc(ctx, ptr, size) data_blob_talloc_named(ctx, ptr, size, "DATA_BLOB: "__location__) + +#if TALLOC_DEPRECATED +#define talloc_zero_p(ctx, type) talloc_zero(ctx, type) +#define talloc_p(ctx, type) talloc(ctx, type) +#define talloc_array_p(ctx, type, count) talloc_array(ctx, type, count) +#define talloc_realloc_p(ctx, p, type, count) talloc_realloc(ctx, p, type, count) +#endif + #ifndef PRINTF_ATTRIBUTE #define PRINTF_ATTRIBUTE(a1, a2) #endif @@ -89,9 +106,9 @@ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIB char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); -void *talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name); -void *talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name); -void *talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name); +void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name); +void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name); +void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name); void *talloc_realloc_fn(const void *context, void *ptr, size_t size); void *talloc_autofree_context(void); diff --git a/source4/lib/talloc/talloc_guide.txt b/source4/lib/talloc/talloc_guide.txt index af6bdf0275..30b7f64d67 100644 --- a/source4/lib/talloc/talloc_guide.txt +++ b/source4/lib/talloc/talloc_guide.txt @@ -5,7 +5,7 @@ Andrew Tridgell September 2004 The most current version of this document is available at - http://samba.org/ftp/unpacked/samba4/talloc_guide.txt + http://samba.org/ftp/unpacked/samba4/source/lib/talloc/talloc_guide.txt If you are used to talloc from Samba3 then please read this carefully, as talloc has changed a lot. @@ -19,7 +19,7 @@ between a "talloc context" and a "talloc pointer". Any pointer returned from talloc() is itself a valid talloc context. This means you can do this: - struct foo *X = talloc_p(mem_ctx, struct foo); + struct foo *X = talloc(mem_ctx, struct foo); X->name = talloc_strdup(X, "foo"); and the pointer X->name would be a "child" of the talloc context "X" @@ -34,7 +34,7 @@ talloc_free(). If you find this confusing, then I suggest you run the LOCAL-TALLOC smbtorture test to watch talloc in action. You may also like to add -your own tests to source/torture/local/talloc.c to clarify how some +your own tests to source/lib/talloc/testsuite.c to clarify how some particular situation is handled. @@ -270,13 +270,13 @@ particularly useful for creating a new temporary working context. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -void *talloc_realloc(const void *context, void *ptr, size_t size); +(type *)talloc_realloc(const void *context, void *ptr, type, count); -The talloc_realloc() function changes the size of a talloc +The talloc_realloc() macro changes the size of a talloc pointer. It has the following equivalences: - talloc_realloc(context, NULL, size) ==> talloc_size(context, size); - talloc_realloc(context, ptr, 0) ==> talloc_free(ptr); + talloc_realloc(context, NULL, type, 1) ==> talloc(context, type); + talloc_realloc(context, ptr, type, 0) ==> talloc_free(ptr); The "context" argument is only used if "ptr" is not NULL, otherwise it is ignored. @@ -287,6 +287,13 @@ more than one parent (see talloc_reference()). =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +void *talloc_realloc_size(const void *context, void *ptr, size_t size); + +the talloc_realloc_size() function is useful when the type is not +known so the typesafe talloc_realloc() cannot be used. + + +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- void *talloc_steal(const void *new_ctx, const void *ptr); The talloc_steal() function changes the parent context of a talloc @@ -403,12 +410,18 @@ full talloc report on 'root' (total 18 bytes in 8 blocks) =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -void *talloc_zero(const void *ctx, size_t size); +(type *)talloc_zero(const void *ctx, type); -The talloc_zero() function is equivalent to: +The talloc_zero() macro is equivalent to: - ptr = talloc_size(ctx, size); - if (ptr) memset(ptr, 0, size); + ptr = talloc(ctx, type); + if (ptr) memset(ptr, 0, sizeof(type)); + + +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +void *talloc_zero_size(const void *ctx, size_t size) + +The talloc_zero_size() function is useful when you don't have a known type =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- @@ -469,25 +482,19 @@ string to the given string. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -void *talloc_array_p(const void *ctx, type, uint_t count); +(type *)talloc_array(const void *ctx, type, uint_t count); -The talloc_array_p() macro is equivalent to: +The talloc_array() macro is equivalent to: (type *)talloc_size(ctx, sizeof(type) * count); except that it provides integer overflow protection for the multiply, returning NULL if the multiply overflows. - =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -void *talloc_realloc_p(const void *ctx, void *ptr, type, uint_t count); - -The talloc_realloc_p() macro is equivalent to: +void *talloc_array_size(const void *ctx, size_t size, uint_t count); - (type *)talloc_realloc(ctx, ptr, sizeof(type) * count); - -except that it provides integer overflow protection for the multiply, -returning NULL if the multiply overflows. +The talloc_array_size() function is useful when the type is not known =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- diff --git a/source4/lib/talloc/testsuite.c b/source4/lib/talloc/testsuite.c index d46964d9b6..e7934a10f3 100644 --- a/source4/lib/talloc/testsuite.c +++ b/source4/lib/talloc/testsuite.c @@ -398,7 +398,7 @@ static BOOL test_misc(void) talloc_report(root, stdout); - p2 = talloc_zero(p1, 20); + p2 = talloc_zero_size(p1, 20); if (p2[19] != 0) { printf("Failed to give zero memory\n"); return False; @@ -520,41 +520,41 @@ static BOOL test_realloc(void) p1 = talloc_size(root, 10); CHECK_SIZE(p1, 10); - p1 = talloc_realloc(NULL, p1, 20); + p1 = talloc_realloc_size(NULL, p1, 20); CHECK_SIZE(p1, 20); talloc_new(p1); - p2 = talloc_realloc(p1, NULL, 30); + p2 = talloc_realloc_size(p1, NULL, 30); talloc_new(p1); - p2 = talloc_realloc(p1, p2, 40); + p2 = talloc_realloc_size(p1, p2, 40); CHECK_SIZE(p2, 40); CHECK_SIZE(root, 60); CHECK_BLOCKS(p1, 4); - p1 = talloc_realloc(NULL, p1, 20); + p1 = talloc_realloc_size(NULL, p1, 20); CHECK_SIZE(p1, 60); talloc_increase_ref_count(p2); - if (talloc_realloc(NULL, p2, 5) != NULL) { + if (talloc_realloc_size(NULL, p2, 5) != NULL) { printf("failed: talloc_realloc() on a referenced pointer should fail\n"); return False; } CHECK_BLOCKS(p1, 4); - talloc_realloc(NULL, p2, 0); - talloc_realloc(NULL, p2, 0); + talloc_realloc_size(NULL, p2, 0); + talloc_realloc_size(NULL, p2, 0); CHECK_BLOCKS(p1, 3); - if (talloc_realloc(NULL, p1, 0x7fffffff) != NULL) { + if (talloc_realloc_size(NULL, p1, 0x7fffffff) != NULL) { printf("failed: oversize talloc should fail\n"); return False; } - talloc_realloc(NULL, p1, 0); + talloc_realloc_size(NULL, p1, 0); CHECK_BLOCKS(root, 1); CHECK_SIZE(root, 0); |