diff options
author | Jeremy Allison <jra@samba.org> | 2004-12-07 18:25:53 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 10:53:32 -0500 |
commit | 620f2e608f70ba92f032720c031283d295c5c06a (patch) | |
tree | ec3dd5fcf29eaa98a26ddeae3acb7a89fd0e0fb8 /source/rpc_parse/parse_misc.c | |
parent | 12440744ba36445186042c8c254785766cce5385 (diff) | |
download | samba-620f2e608f70ba92f032720c031283d295c5c06a.tar.gz samba-620f2e608f70ba92f032720c031283d295c5c06a.tar.xz samba-620f2e608f70ba92f032720c031283d295c5c06a.zip |
r4088: Get medieval on our ass about malloc.... :-). Take control of all our allocation
functions so we can funnel through some well known functions. Should help greatly with
malloc checking.
HEAD patch to follow.
Jeremy.
Diffstat (limited to 'source/rpc_parse/parse_misc.c')
-rw-r--r-- | source/rpc_parse/parse_misc.c | 80 |
1 files changed, 30 insertions, 50 deletions
diff --git a/source/rpc_parse/parse_misc.c b/source/rpc_parse/parse_misc.c index c6f05df0c47..17a8f624aee 100644 --- a/source/rpc_parse/parse_misc.c +++ b/source/rpc_parse/parse_misc.c @@ -552,18 +552,14 @@ void init_unistr(UNISTR *str, const char *buf) return; } - len = strlen(buf) + 1; + len = MAX(len,MAX_UNISTRLEN); - if (len < MAX_UNISTRLEN) - len = MAX_UNISTRLEN; - len *= sizeof(uint16); - - str->buffer = (uint16 *)talloc_zero(get_talloc_ctx(), len); + str->buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, len); if (str->buffer == NULL) smb_panic("init_unistr: malloc fail\n"); - rpcstr_push(str->buffer, buf, len, STR_TERMINATE); + rpcstr_push(str->buffer, buf, len*sizeof(uint16), STR_TERMINATE); } /******************************************************************* @@ -591,10 +587,9 @@ BOOL smb_io_unistr(const char *desc, UNISTR *uni, prs_struct *ps, int depth) static void create_buffer3(BUFFER3 *str, size_t len) { - if (len < MAX_BUFFERLEN) - len = MAX_BUFFERLEN; + len = MAX(len,MAX_BUFFERLEN); - str->buffer = talloc_zero(get_talloc_ctx(), len); + str->buffer = TALLOC_ZERO(get_talloc_ctx(), len); if (str->buffer == NULL) smb_panic("create_buffer3: talloc fail\n"); @@ -683,7 +678,7 @@ BOOL smb_io_buffer3(const char *desc, BUFFER3 *buf3, prs_struct *ps, int depth) return False; if (UNMARSHALLING(ps)) { - buf3->buffer = (unsigned char *)prs_alloc_mem(ps, buf3->buf_max_len); + buf3->buffer = PRS_ALLOC_MEM(ps, unsigned char, buf3->buf_max_len); if (buf3->buffer == NULL) return False; } @@ -735,9 +730,8 @@ void init_buffer2(BUFFER2 *str, const uint8 *buf, size_t len) str->buf_len = buf != NULL ? len : 0; if (buf != NULL) { - if (len < MAX_BUFFERLEN) - len = MAX_BUFFERLEN; - str->buffer = talloc_zero(get_talloc_ctx(), len); + len = MAX(len,MAX_BUFFERLEN); + str->buffer = TALLOC_ZERO(get_talloc_ctx(), len); if (str->buffer == NULL) smb_panic("init_buffer2: talloc fail\n"); memcpy(str->buffer, buf, MIN(str->buf_len, len)); @@ -819,14 +813,9 @@ void copy_unistr2(UNISTR2 *str, const UNISTR2 *from) (the the length of the source string) to prevent reallocation of memory. */ if (str->buffer == NULL) { - size_t len = from->uni_max_len * sizeof(uint16); - - if (len < MAX_UNISTRLEN) - len = MAX_UNISTRLEN; - len *= sizeof(uint16); - - str->buffer = (uint16 *)talloc_zero(get_talloc_ctx(), len); - if ((str->buffer == NULL) && (len > 0 )) { + size_t alloc_len = MAX(from->uni_max_len,MAX_UNISTRLEN); + str->buffer = (uint16 *)TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, alloc_len); + if ((str->buffer == NULL)) { smb_panic("copy_unistr2: talloc fail\n"); return; } @@ -842,8 +831,6 @@ void copy_unistr2(UNISTR2 *str, const UNISTR2 *from) void init_string2(STRING2 *str, const char *buf, int max_len, int str_len) { - int alloc_len = 0; - /* set up string lengths. */ str->str_max_len = max_len; str->offset = 0; @@ -851,9 +838,8 @@ void init_string2(STRING2 *str, const char *buf, int max_len, int str_len) /* store the string */ if(str_len != 0) { - if (str_len < MAX_STRINGLEN) - alloc_len = MAX_STRINGLEN; - str->buffer = talloc_zero(get_talloc_ctx(), alloc_len); + int alloc_len = MAX(str_len, MAX_STRINGLEN); + str->buffer = TALLOC_ZERO(get_talloc_ctx(), alloc_len); if (str->buffer == NULL) smb_panic("init_string2: malloc fail\n"); memcpy(str->buffer, buf, str_len); @@ -917,16 +903,17 @@ void init_unistr2(UNISTR2 *str, const char *buf, enum unistr2_term_codes flags) len = strlen(buf) + 1; } - if (len < MAX_UNISTRLEN) - len = MAX_UNISTRLEN; - len *= sizeof(uint16); + len = MAX(len,MAX_UNISTRLEN); - str->buffer = (uint16 *)talloc_zero(get_talloc_ctx(), len); - if ((str->buffer == NULL) && (len > 0)) { + str->buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, len); + if (str->buffer == NULL) { smb_panic("init_unistr2: malloc fail\n"); return; } + /* Ensure len is the length in *bytes* */ + len *= sizeof(uint16); + /* * The UNISTR2 must be initialized !!! * jfm, 7/7/2001. @@ -956,7 +943,6 @@ void init_unistr2(UNISTR2 *str, const char *buf, enum unistr2_term_codes flags) void init_unistr2_w(TALLOC_CTX *ctx, UNISTR2 *str, const smb_ucs2_t *buf) { uint32 len = strlen_w(buf); - uint32 max_len = len; uint32 alloc_len; ZERO_STRUCTP(str); @@ -966,13 +952,10 @@ void init_unistr2_w(TALLOC_CTX *ctx, UNISTR2 *str, const smb_ucs2_t *buf) str->offset = 0; str->uni_str_len = len; - if (max_len < MAX_UNISTRLEN) - max_len = MAX_UNISTRLEN; + alloc_len = MAX((len + 1), MAX_UNISTRLEN); - alloc_len = (max_len + 1) * sizeof(uint16); - - str->buffer = (uint16 *)talloc_zero(ctx, alloc_len); - if ((str->buffer == NULL) && (alloc_len > 0)) { + str->buffer = TALLOC_ZERO_ARRAY(ctx, uint16, alloc_len); + if (str->buffer == NULL) { smb_panic("init_unistr2_w: malloc fail\n"); return; } @@ -1021,10 +1004,10 @@ void init_unistr2_from_unistr(UNISTR2 *to, const UNISTR *from) to->uni_str_len = i; /* allocate the space and copy the string buffer */ - to->buffer = (uint16 *)talloc_zero(get_talloc_ctx(), sizeof(uint16)*(to->uni_str_len)); + to->buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, i); if (to->buffer == NULL) smb_panic("init_unistr2_from_unistr: malloc fail\n"); - memcpy(to->buffer, from->buffer, to->uni_max_len*sizeof(uint16)); + memcpy(to->buffer, from->buffer, i*sizeof(uint16)); return; } @@ -1111,7 +1094,7 @@ BOOL init_unistr2_array(UNISTR2_ARRAY *array, return True; } - array->strings = (UNISTR2_ARRAY_EL *)talloc_zero(get_talloc_ctx(), count * sizeof(UNISTR2_ARRAY_EL)); + array->strings = TALLOC_ZERO_ARRAY(get_talloc_ctx(), UNISTR2_ARRAY_EL, count ); if (!array->strings) { return False; } @@ -1151,7 +1134,7 @@ BOOL smb_io_unistr2_array(const char *desc, UNISTR2_ARRAY *array, prs_struct *ps } if (UNMARSHALLING(ps)) { - array->strings = talloc_zero(get_talloc_ctx(), array->count * sizeof(array->strings[0])); + array->strings = TALLOC_ZERO_ARRAY(get_talloc_ctx(), UNISTR2_ARRAY_EL, array->count ); } if (! array->strings) { return False; @@ -1637,7 +1620,7 @@ BOOL smb_io_pol_hnd(const char *desc, POLICY_HND *pol, prs_struct *ps, int depth void init_unistr3(UNISTR3 *str, const char *buf) { - size_t len; + size_t len, alloc_len; if (buf == NULL) { str->uni_str_len=0; @@ -1649,16 +1632,13 @@ void init_unistr3(UNISTR3 *str, const char *buf) str->uni_str_len=len; - if (len < MAX_UNISTRLEN) - len = MAX_UNISTRLEN; - - len *= sizeof(uint16); + alloc_len = MAX(len, MAX_UNISTRLEN); - str->str.buffer = (uint16 *)talloc_zero(get_talloc_ctx(), len); + str->str.buffer = TALLOC_ZERO_ARRAY(get_talloc_ctx(), uint16, alloc_len); if (str->str.buffer == NULL) smb_panic("init_unistr3: malloc fail\n"); - rpcstr_push((char *)str->str.buffer, buf, len, STR_TERMINATE); + rpcstr_push((char *)str->str.buffer, buf, len * sizeof(uint16), STR_TERMINATE); } /******************************************************************* |