Print this page
11227 smb code needs smatch fixes


   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  23  */
  24 




  25 #include <sys/types.h>
  26 #include <sys/sunddi.h>
  27 #include <sys/kmem.h>
  28 #include <sys/sysmacros.h>
  29 #include <smbsrv/smb_kproto.h>
  30 #include <smbsrv/alloc.h>
  31 
  32 #define SMB_SMH_MAGIC           0x534D485F      /* 'SMH_' */
  33 #define SMB_SMH_VALID(_smh_)    ASSERT((_smh_)->smh_magic == SMB_SMH_MAGIC)
  34 #define SMB_MEM2SMH(_mem_)      ((smb_mem_header_t *)(_mem_) - 1)
  35 
  36 typedef struct smb_mem_header {
  37         uint32_t        smh_magic;
  38         size_t          smh_size;
  39         smb_request_t   *smh_sr;
  40         list_node_t     smh_lnd;
  41 } smb_mem_header_t;
  42 
  43 static void *smb_alloc(smb_request_t *, size_t, boolean_t);
  44 static void smb_free(smb_request_t *, void *, boolean_t);


 278  * zero If true zero out the extra space or the truncated space.
 279  */
 280 static void *
 281 smb_realloc(smb_request_t *sr, void *ptr, size_t size, boolean_t zero)
 282 {
 283         smb_mem_header_t        *smh;
 284         void                    *new_ptr;
 285 
 286         if (ptr == NULL)
 287                 return (smb_alloc(sr, size, zero));
 288 
 289         smh = SMB_MEM2SMH(ptr);
 290         SMB_SMH_VALID(smh);
 291         ASSERT(sr == smh->smh_sr);
 292 
 293         if (size == 0) {
 294                 smb_free(sr, ptr, zero);
 295                 return (NULL);
 296         }
 297         if (smh->smh_size >= size) {
 298                 if ((zero) & (smh->smh_size > size))
 299                         bzero((caddr_t)ptr + size, smh->smh_size - size);
 300                 return (ptr);
 301         }
 302         new_ptr = smb_alloc(sr, size, B_FALSE);
 303         bcopy(ptr, new_ptr, smh->smh_size);
 304         if (zero)
 305                 bzero((caddr_t)new_ptr + smh->smh_size, size - smh->smh_size);
 306 
 307         smb_free(sr, ptr, zero);
 308         return (new_ptr);
 309 }


   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  23  */
  24 
  25 /*
  26  * Copyright 2019 Joyent, Inc.
  27  */
  28 
  29 #include <sys/types.h>
  30 #include <sys/sunddi.h>
  31 #include <sys/kmem.h>
  32 #include <sys/sysmacros.h>
  33 #include <smbsrv/smb_kproto.h>
  34 #include <smbsrv/alloc.h>
  35 
  36 #define SMB_SMH_MAGIC           0x534D485F      /* 'SMH_' */
  37 #define SMB_SMH_VALID(_smh_)    ASSERT((_smh_)->smh_magic == SMB_SMH_MAGIC)
  38 #define SMB_MEM2SMH(_mem_)      ((smb_mem_header_t *)(_mem_) - 1)
  39 
  40 typedef struct smb_mem_header {
  41         uint32_t        smh_magic;
  42         size_t          smh_size;
  43         smb_request_t   *smh_sr;
  44         list_node_t     smh_lnd;
  45 } smb_mem_header_t;
  46 
  47 static void *smb_alloc(smb_request_t *, size_t, boolean_t);
  48 static void smb_free(smb_request_t *, void *, boolean_t);


 282  * zero If true zero out the extra space or the truncated space.
 283  */
 284 static void *
 285 smb_realloc(smb_request_t *sr, void *ptr, size_t size, boolean_t zero)
 286 {
 287         smb_mem_header_t        *smh;
 288         void                    *new_ptr;
 289 
 290         if (ptr == NULL)
 291                 return (smb_alloc(sr, size, zero));
 292 
 293         smh = SMB_MEM2SMH(ptr);
 294         SMB_SMH_VALID(smh);
 295         ASSERT(sr == smh->smh_sr);
 296 
 297         if (size == 0) {
 298                 smb_free(sr, ptr, zero);
 299                 return (NULL);
 300         }
 301         if (smh->smh_size >= size) {
 302                 if ((zero) && (smh->smh_size > size))
 303                         bzero((caddr_t)ptr + size, smh->smh_size - size);
 304                 return (ptr);
 305         }
 306         new_ptr = smb_alloc(sr, size, B_FALSE);
 307         bcopy(ptr, new_ptr, smh->smh_size);
 308         if (zero)
 309                 bzero((caddr_t)new_ptr + smh->smh_size, size - smh->smh_size);
 310 
 311         smb_free(sr, ptr, zero);
 312         return (new_ptr);
 313 }