Print this page
5679 be_sort_list(): Possible null pointer dereference

Split Close
Expand all
Collapse all
          --- old/usr/src/lib/libbe/common/be_list.c
          +++ new/usr/src/lib/libbe/common/be_list.c
↓ open down ↓ 18 lines elided ↑ open up ↑
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  24   24   */
  25   25  
  26   26  /*
  27   27   * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
  28   28   * Copyright 2015 Toomas Soome <tsoome@me.com>
       29 + * Copyright 2015 Gary Mills
  29   30   */
  30   31  
  31   32  #include <assert.h>
  32   33  #include <libintl.h>
  33   34  #include <libnvpair.h>
  34   35  #include <libzfs.h>
  35   36  #include <stdio.h>
  36   37  #include <stdlib.h>
  37   38  #include <string.h>
  38   39  #include <strings.h>
↓ open down ↓ 651 lines elided ↑ open up ↑
 690  691   *              node list sorted by name
 691  692   * Scope:
 692  693   *              Private
 693  694   */
 694  695  static void
 695  696  be_sort_list(be_node_list_t **pstart, int (*compar)(const void *, const void *))
 696  697  {
 697  698          size_t ibe, nbe;
 698  699          be_node_list_t *p = NULL;
 699  700          be_node_list_t **ptrlist = NULL;
      701 +        be_node_list_t **ptrtmp;
 700  702  
 701  703          if (pstart == NULL)
 702  704                  return;
 703  705          /* build array of linked list BE struct pointers */
 704  706          for (p = *pstart, nbe = 0; p != NULL; nbe++, p = p->be_next_node) {
 705      -                ptrlist = realloc(ptrlist,
      707 +                ptrtmp = realloc(ptrlist,
 706  708                      sizeof (be_node_list_t *) * (nbe + 2));
      709 +                if (ptrtmp == NULL) { /* out of memory */
      710 +                        be_print_err(gettext("be_sort_list: memory "
      711 +                            "allocation failed\n"));
      712 +                        goto free;
      713 +                }
      714 +                ptrlist = ptrtmp;
 707  715                  ptrlist[nbe] = p;
 708  716          }
 709  717          if (nbe == 0)
 710  718                  return;
 711  719          /* in-place list quicksort using qsort(3C) */
 712  720          if (nbe > 1)    /* no sort if less than 2 BEs */
 713  721                  qsort(ptrlist, nbe, sizeof (be_node_list_t *), compar);
 714  722  
 715  723          ptrlist[nbe] = NULL; /* add linked list terminator */
 716  724          *pstart = ptrlist[0]; /* set new linked list header */
↓ open down ↓ 647 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX