summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordoug <>2015-06-13 09:11:57 +0000
committerdoug <>2015-06-13 09:11:57 +0000
commit07704d0548b532c528371e46172c816801fe6b6d (patch)
treec1fd384daf3675678e1f586286098659c791ee5d /src
parent159f76928fe13123fd28148a0ad396034f1a1f8f (diff)
downloadopenbsd-07704d0548b532c528371e46172c816801fe6b6d.tar.gz
openbsd-07704d0548b532c528371e46172c816801fe6b6d.tar.bz2
openbsd-07704d0548b532c528371e46172c816801fe6b6d.zip
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@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libssl/bs_cbb.c15
-rw-r--r--src/lib/libssl/src/ssl/bs_cbb.c15
2 files changed, 16 insertions, 14 deletions
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 @@
1/* $OpenBSD: bs_cbb.c,v 1.8 2015/04/29 02:02:46 doug Exp $ */ 1/* $OpenBSD: bs_cbb.c,v 1.9 2015/06/13 09:11:57 doug Exp $ */
2/* 2/*
3 * Copyright (c) 2014, Google Inc. 3 * Copyright (c) 2014, Google Inc.
4 * 4 *
@@ -36,7 +36,7 @@ cbb_init(CBB *cbb, uint8_t *buf, size_t cap)
36 base->cap = cap; 36 base->cap = cap;
37 base->can_resize = 1; 37 base->can_resize = 1;
38 38
39 memset(cbb, 0, sizeof(CBB)); 39 memset(cbb, 0, sizeof(*cbb));
40 cbb->base = base; 40 cbb->base = base;
41 cbb->is_top_level = 1; 41 cbb->is_top_level = 1;
42 return 1; 42 return 1;
@@ -45,11 +45,12 @@ cbb_init(CBB *cbb, uint8_t *buf, size_t cap)
45int 45int
46CBB_init(CBB *cbb, size_t initial_capacity) 46CBB_init(CBB *cbb, size_t initial_capacity)
47{ 47{
48 uint8_t *buf; 48 uint8_t *buf = NULL;
49 49
50 buf = malloc(initial_capacity); 50 if (initial_capacity > 0) {
51 if (initial_capacity > 0 && buf == NULL) 51 if ((buf = malloc(initial_capacity)) == NULL)
52 return 0; 52 return 0;
53 }
53 54
54 if (!cbb_init(cbb, buf, initial_capacity)) { 55 if (!cbb_init(cbb, buf, initial_capacity)) {
55 free(buf); 56 free(buf);
@@ -72,7 +73,7 @@ void
72CBB_cleanup(CBB *cbb) 73CBB_cleanup(CBB *cbb)
73{ 74{
74 if (cbb->base) { 75 if (cbb->base) {
75 if (cbb->base->buf && cbb->base->can_resize) 76 if (cbb->base->can_resize)
76 free(cbb->base->buf); 77 free(cbb->base->buf);
77 78
78 free(cbb->base); 79 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 @@
1/* $OpenBSD: bs_cbb.c,v 1.8 2015/04/29 02:02:46 doug Exp $ */ 1/* $OpenBSD: bs_cbb.c,v 1.9 2015/06/13 09:11:57 doug Exp $ */
2/* 2/*
3 * Copyright (c) 2014, Google Inc. 3 * Copyright (c) 2014, Google Inc.
4 * 4 *
@@ -36,7 +36,7 @@ cbb_init(CBB *cbb, uint8_t *buf, size_t cap)
36 base->cap = cap; 36 base->cap = cap;
37 base->can_resize = 1; 37 base->can_resize = 1;
38 38
39 memset(cbb, 0, sizeof(CBB)); 39 memset(cbb, 0, sizeof(*cbb));
40 cbb->base = base; 40 cbb->base = base;
41 cbb->is_top_level = 1; 41 cbb->is_top_level = 1;
42 return 1; 42 return 1;
@@ -45,11 +45,12 @@ cbb_init(CBB *cbb, uint8_t *buf, size_t cap)
45int 45int
46CBB_init(CBB *cbb, size_t initial_capacity) 46CBB_init(CBB *cbb, size_t initial_capacity)
47{ 47{
48 uint8_t *buf; 48 uint8_t *buf = NULL;
49 49
50 buf = malloc(initial_capacity); 50 if (initial_capacity > 0) {
51 if (initial_capacity > 0 && buf == NULL) 51 if ((buf = malloc(initial_capacity)) == NULL)
52 return 0; 52 return 0;
53 }
53 54
54 if (!cbb_init(cbb, buf, initial_capacity)) { 55 if (!cbb_init(cbb, buf, initial_capacity)) {
55 free(buf); 56 free(buf);
@@ -72,7 +73,7 @@ void
72CBB_cleanup(CBB *cbb) 73CBB_cleanup(CBB *cbb)
73{ 74{
74 if (cbb->base) { 75 if (cbb->base) {
75 if (cbb->base->buf && cbb->base->can_resize) 76 if (cbb->base->can_resize)
76 free(cbb->base->buf); 77 free(cbb->base->buf);
77 78
78 free(cbb->base); 79 free(cbb->base);