From: doug Date: Sat, 13 Jun 2015 09:11:57 +0000 (+0000) Subject: When initial capacity is 0, always use NULL buffer. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=3a0c64b9b695c943cdd17b6af41e5965d5e88ae1;p=openbsd When initial capacity is 0, always use NULL buffer. malloc(0) is implementation defined and there's no reason to introduce that ambiguity here. Added a few cosmetic changes in sizeof and free. ok miod@ jsing@ --- diff --git a/lib/libssl/bs_cbb.c b/lib/libssl/bs_cbb.c index b766e850d33..29312e104b4 100644 --- a/lib/libssl/bs_cbb.c +++ b/lib/libssl/bs_cbb.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bs_cbb.c,v 1.8 2015/04/29 02:02:46 doug Exp $ */ +/* $OpenBSD: bs_cbb.c,v 1.9 2015/06/13 09:11:57 doug Exp $ */ /* * Copyright (c) 2014, Google Inc. * @@ -36,7 +36,7 @@ cbb_init(CBB *cbb, uint8_t *buf, size_t cap) base->cap = cap; base->can_resize = 1; - memset(cbb, 0, sizeof(CBB)); + memset(cbb, 0, sizeof(*cbb)); cbb->base = base; cbb->is_top_level = 1; return 1; @@ -45,11 +45,12 @@ cbb_init(CBB *cbb, uint8_t *buf, size_t cap) int CBB_init(CBB *cbb, size_t initial_capacity) { - uint8_t *buf; + uint8_t *buf = NULL; - buf = malloc(initial_capacity); - if (initial_capacity > 0 && buf == NULL) - return 0; + if (initial_capacity > 0) { + if ((buf = malloc(initial_capacity)) == NULL) + return 0; + } if (!cbb_init(cbb, buf, initial_capacity)) { free(buf); @@ -72,7 +73,7 @@ void CBB_cleanup(CBB *cbb) { if (cbb->base) { - if (cbb->base->buf && cbb->base->can_resize) + if (cbb->base->can_resize) free(cbb->base->buf); free(cbb->base); diff --git a/lib/libssl/src/ssl/bs_cbb.c b/lib/libssl/src/ssl/bs_cbb.c index b766e850d33..29312e104b4 100644 --- a/lib/libssl/src/ssl/bs_cbb.c +++ b/lib/libssl/src/ssl/bs_cbb.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bs_cbb.c,v 1.8 2015/04/29 02:02:46 doug Exp $ */ +/* $OpenBSD: bs_cbb.c,v 1.9 2015/06/13 09:11:57 doug Exp $ */ /* * Copyright (c) 2014, Google Inc. * @@ -36,7 +36,7 @@ cbb_init(CBB *cbb, uint8_t *buf, size_t cap) base->cap = cap; base->can_resize = 1; - memset(cbb, 0, sizeof(CBB)); + memset(cbb, 0, sizeof(*cbb)); cbb->base = base; cbb->is_top_level = 1; return 1; @@ -45,11 +45,12 @@ cbb_init(CBB *cbb, uint8_t *buf, size_t cap) int CBB_init(CBB *cbb, size_t initial_capacity) { - uint8_t *buf; + uint8_t *buf = NULL; - buf = malloc(initial_capacity); - if (initial_capacity > 0 && buf == NULL) - return 0; + if (initial_capacity > 0) { + if ((buf = malloc(initial_capacity)) == NULL) + return 0; + } if (!cbb_init(cbb, buf, initial_capacity)) { free(buf); @@ -72,7 +73,7 @@ void CBB_cleanup(CBB *cbb) { if (cbb->base) { - if (cbb->base->buf && cbb->base->can_resize) + if (cbb->base->can_resize) free(cbb->base->buf); free(cbb->base);