From 07704d0548b532c528371e46172c816801fe6b6d Mon Sep 17 00:00:00 2001 From: doug <> Date: Sat, 13 Jun 2015 09:11:57 +0000 Subject: 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@ --- src/lib/libssl/bs_cbb.c | 15 ++++++++------- src/lib/libssl/src/ssl/bs_cbb.c | 15 ++++++++------- 2 files changed, 16 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/lib/libssl/bs_cbb.c b/src/lib/libssl/bs_cbb.c index b766e850d3..29312e104b 100644 --- a/src/lib/libssl/bs_cbb.c +++ b/src/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/src/lib/libssl/src/ssl/bs_cbb.c b/src/lib/libssl/src/ssl/bs_cbb.c index b766e850d3..29312e104b 100644 --- a/src/lib/libssl/src/ssl/bs_cbb.c +++ b/src/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); -- cgit v1.2.3-55-g6feb