[GLUE] Rsync SAMBA_3_2_0 SVN r25598 in order to create the v3-2-test branch.
[samba.git] / source / rpc_parse / parse_prs.c
index 84f64a2a6635d7937e3e9a8039725fdf7d1f9e0a..b22b1faa3fba6ca4a6dec3c9978662af74870c2a 100644 (file)
@@ -8,7 +8,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -17,8 +17,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
@@ -123,6 +122,10 @@ BOOL prs_init(prs_struct *ps, uint32 size, TALLOC_CTX *ctx, BOOL io)
 
 /*******************************************************************
  Delete the memory in a parse structure - if we own it.
+
+ NOTE: Contrary to the somewhat confusing naming, this function is not
+       intended for freeing memory allocated by prs_alloc_mem().  That memory
+       is attached to the talloc context given by ps->mem_ctx.
  ********************************************************************/
 
 void prs_mem_free(prs_struct *ps)
@@ -158,7 +161,7 @@ char *prs_alloc_mem(prs_struct *ps, size_t size, unsigned int count)
 
        if (size && count) {
                /* We can't call the type-safe version here. */
-               ret = (char *)_talloc_zero_array_zeronull(ps->mem_ctx, size, count,
+               ret = (char *)_talloc_zero_array(ps->mem_ctx, size, count,
                                                 "parse_prs");
        }
        return ret;
@@ -644,8 +647,12 @@ BOOL prs_pointer( const char *name, prs_struct *ps, int depth,
                return True;
 
        if (UNMARSHALLING(ps)) {
-               if ( !(*data = PRS_ALLOC_MEM(ps, char, data_size)) )
-                       return False;
+               if (data_size) {
+                       if ( !(*data = PRS_ALLOC_MEM(ps, char, data_size)) )
+                               return False;
+               } else {
+                       *data = NULL;
+               }
        }
 
        return prs_fn(name, ps, depth, *data);
@@ -1016,16 +1023,16 @@ BOOL prs_buffer5(BOOL charmode, const char *name, prs_struct *ps, int depth, BUF
        if (q == NULL)
                return False;
 
+       /* If the string is empty, we don't have anything to stream */
+       if (str->buf_len==0)
+               return True;
+
        if (UNMARSHALLING(ps)) {
                str->buffer = PRS_ALLOC_MEM(ps,uint16,str->buf_len);
                if (str->buffer == NULL)
                        return False;
        }
 
-       /* If the string is empty, we don't have anything to stream */
-       if (str->buf_len==0)
-               return True;
-
        p = (char *)str->buffer;
 
        dbg_rw_punival(charmode, name, depth, ps, q, p, str->buf_len);
@@ -1055,6 +1062,8 @@ BOOL prs_regval_buffer(BOOL charmode, const char *name, prs_struct *ps, int dept
                        buf->buffer = PRS_ALLOC_MEM(ps, uint16, buf->buf_max_len);
                        if ( buf->buffer == NULL )
                                return False;
+               } else {
+                       buf->buffer = NULL;
                }
        }
 
@@ -1082,9 +1091,16 @@ BOOL prs_string2(BOOL charmode, const char *name, prs_struct *ps, int depth, STR
                if (str->str_str_len > str->str_max_len) {
                        return False;
                }
-               str->buffer = PRS_ALLOC_MEM(ps,unsigned char, str->str_max_len);
-               if (str->buffer == NULL)
-                       return False;
+               if (str->str_max_len) {
+                       str->buffer = PRS_ALLOC_MEM(ps,unsigned char, str->str_max_len);
+                       if (str->buffer == NULL)
+                               return False;
+               } else {
+                       str->buffer = NULL;
+                       /* Return early to ensure Coverity isn't confused. */
+                       DEBUG(5,("%s%04x %s: \n", tab_depth(depth), ps->data_offset, name));
+                       return True;
+               }
        }
 
        if (UNMARSHALLING(ps)) {
@@ -1129,9 +1145,13 @@ BOOL prs_unistr2(BOOL charmode, const char *name, prs_struct *ps, int depth, UNI
                if (str->uni_str_len > str->uni_max_len) {
                        return False;
                }
-               str->buffer = PRS_ALLOC_MEM(ps,uint16,str->uni_max_len);
-               if (str->buffer == NULL)
-                       return False;
+               if (str->uni_max_len) {
+                       str->buffer = PRS_ALLOC_MEM(ps,uint16,str->uni_max_len);
+                       if (str->buffer == NULL)
+                               return False;
+               } else {
+                       str->buffer = NULL;
+               }
        }
 
        p = (char *)str->buffer;
@@ -1156,9 +1176,13 @@ BOOL prs_unistr3(BOOL charmode, const char *name, UNISTR3 *str, prs_struct *ps,
                return False;
 
        if (UNMARSHALLING(ps)) {
-               str->str.buffer = PRS_ALLOC_MEM(ps,uint16,str->uni_str_len);
-               if (str->str.buffer == NULL)
-                       return False;
+               if (str->uni_str_len) {
+                       str->str.buffer = PRS_ALLOC_MEM(ps,uint16,str->uni_str_len);
+                       if (str->str.buffer == NULL)
+                               return False;
+               } else {
+                       str->str.buffer = NULL;
+               }
        }
 
        p = (char *)str->str.buffer;
@@ -1462,12 +1486,6 @@ int tdb_prs_store(TDB_CONTEXT *tdb, TDB_DATA kbuf, prs_struct *ps)
        return tdb_trans_store(tdb, kbuf, dbuf, TDB_REPLACE);
 }
 
-int tdb_prs_store_bystring(TDB_CONTEXT *tdb, char *keystr, prs_struct *ps)
-{
-       TDB_DATA kbuf = string_term_tdb_data(keystr);
-       return tdb_prs_store(tdb, kbuf, ps);
-}
-
 /* useful function to fetch a structure into rpc wire format */
 int tdb_prs_fetch(TDB_CONTEXT *tdb, TDB_DATA kbuf, prs_struct *ps, TALLOC_CTX *mem_ctx)
 {
@@ -1482,12 +1500,6 @@ int tdb_prs_fetch(TDB_CONTEXT *tdb, TDB_DATA kbuf, prs_struct *ps, TALLOC_CTX *m
        prs_give_memory(ps, (char *)dbuf.dptr, dbuf.dsize, True);
 
        return 0;
-} 
-
-int tdb_prs_fetch_bystring(TDB_CONTEXT *tdb, char *keystr, prs_struct *ps, TALLOC_CTX *mem_ctx)
-{
-       TDB_DATA kbuf = string_term_tdb_data(keystr);
-       return tdb_prs_fetch(tdb, kbuf, ps, mem_ctx);
 }
 
 /*******************************************************************
@@ -1527,7 +1539,7 @@ static void schannel_digest(struct schannel_auth_struct *a,
                          uchar digest_final[16]) 
 {
        uchar whole_packet_digest[16];
-       static uchar zeros[4];
+       static const uchar zeros[4] = { 0, };
        struct MD5Context ctx3;
        
        /* verfiy the signature on the packet by MD5 over various bits */
@@ -1556,7 +1568,7 @@ static void schannel_get_sealing_key(struct schannel_auth_struct *a,
                                   RPC_AUTH_SCHANNEL_CHK *verf,
                                   uchar sealing_key[16]) 
 {
-       static uchar zeros[4];
+       static const uchar zeros[4] = { 0, };
        uchar digest2[16];
        uchar sess_kf0[16];
        int i;
@@ -1583,7 +1595,7 @@ static void schannel_get_sealing_key(struct schannel_auth_struct *a,
 static void schannel_deal_with_seq_num(struct schannel_auth_struct *a,
                                     RPC_AUTH_SCHANNEL_CHK *verf)
 {
-       static uchar zeros[4];
+       static const uchar zeros[4] = { 0, };
        uchar sequence_key[16];
        uchar digest1[16];
 
@@ -1754,9 +1766,9 @@ BOOL schannel_decode(struct schannel_auth_struct *a, enum pipe_auth_level auth_l
                   checksum after the decode, below
                */
                DEBUG(2, ("schannel_decode: FAILED: packet sequence number:\n"));
-               dump_data(2, (const uint8 *)verf->seq_num, sizeof(verf->seq_num));
+               dump_data(2, verf->seq_num, sizeof(verf->seq_num));
                DEBUG(2, ("should be:\n"));
-               dump_data(2, (const uint8 *)seq_num, sizeof(seq_num));
+               dump_data(2, seq_num, sizeof(seq_num));
 
                return False;
        }
@@ -1764,9 +1776,9 @@ BOOL schannel_decode(struct schannel_auth_struct *a, enum pipe_auth_level auth_l
        if (memcmp(verf->sig, schannel_sig, sizeof(verf->sig))) {
                /* Validate that the other end sent the expected header */
                DEBUG(2, ("schannel_decode: FAILED: packet header:\n"));
-               dump_data(2, (const uint8 *)verf->sig, sizeof(verf->sig));
+               dump_data(2, verf->sig, sizeof(verf->sig));
                DEBUG(2, ("should be:\n"));
-               dump_data(2, (const uint8 *)schannel_sig, sizeof(schannel_sig));
+               dump_data(2, schannel_sig, sizeof(schannel_sig));
                return False;
        }