comparison circbuffer.c @ 1173:d734fe76b72f coverity

merge
author Matt Johnston <matt@ucc.asn.au>
date Mon, 23 Nov 2015 23:04:48 +0800
parents 1397a677cb5c
children 06d52bcb8094
comparison
equal deleted inserted replaced
1144:624fc24cfae5 1173:d734fe76b72f
35 if (size > MAX_CBUF_SIZE) { 35 if (size > MAX_CBUF_SIZE) {
36 dropbear_exit("Bad cbuf size"); 36 dropbear_exit("Bad cbuf size");
37 } 37 }
38 38
39 cbuf = (circbuffer*)m_malloc(sizeof(circbuffer)); 39 cbuf = (circbuffer*)m_malloc(sizeof(circbuffer));
40 if (size > 0) { 40 /* data is malloced on first write */
41 cbuf->data = (unsigned char*)m_malloc(size); 41 cbuf->data = NULL;
42 }
43 cbuf->used = 0; 42 cbuf->used = 0;
44 cbuf->readpos = 0; 43 cbuf->readpos = 0;
45 cbuf->writepos = 0; 44 cbuf->writepos = 0;
46 cbuf->size = size; 45 cbuf->size = size;
47 46
48 return cbuf; 47 return cbuf;
49 } 48 }
50 49
51 void cbuf_free(circbuffer * cbuf) { 50 void cbuf_free(circbuffer * cbuf) {
52 51
53 m_burn(cbuf->data, cbuf->size); 52 if (cbuf->data) {
54 m_free(cbuf->data); 53 m_burn(cbuf->data, cbuf->size);
54 m_free(cbuf->data);
55 }
55 m_free(cbuf); 56 m_free(cbuf);
56 } 57 }
57 58
58 unsigned int cbuf_getused(circbuffer * cbuf) { 59 unsigned int cbuf_getused(circbuffer * cbuf) {
59 60
104 105
105 if (len > cbuf_writelen(cbuf)) { 106 if (len > cbuf_writelen(cbuf)) {
106 dropbear_exit("Bad cbuf write"); 107 dropbear_exit("Bad cbuf write");
107 } 108 }
108 109
110 if (!cbuf->data) {
111 /* lazy allocation */
112 cbuf->data = (unsigned char*)m_malloc(cbuf->size);
113 }
114
109 return &cbuf->data[cbuf->writepos]; 115 return &cbuf->data[cbuf->writepos];
110 } 116 }
111 117
112 void cbuf_incrwrite(circbuffer *cbuf, unsigned int len) { 118 void cbuf_incrwrite(circbuffer *cbuf, unsigned int len) {
113 if (len > cbuf_writelen(cbuf)) { 119 if (len > cbuf_writelen(cbuf)) {