comparison buffer.c @ 1747:ff51d5967e2d

Avoid passing NULL to memcpy
author Matt Johnston <matt@ucc.asn.au>
date Mon, 19 Oct 2020 21:38:20 +0800
parents 1051e4eea25a
children 064f5be2fc45
comparison
equal deleted inserted replaced
1746:28ab2cdb84bf 1747:ff51d5967e2d
37 /* avoid excessively large numbers, > ~8192 bits */ 37 /* avoid excessively large numbers, > ~8192 bits */
38 #define BUF_MAX_MPINT (8240 / 8) 38 #define BUF_MAX_MPINT (8240 / 8)
39 39
40 /* Create (malloc) a new buffer of size */ 40 /* Create (malloc) a new buffer of size */
41 buffer* buf_new(unsigned int size) { 41 buffer* buf_new(unsigned int size) {
42
43 buffer* buf; 42 buffer* buf;
44
45 if (size > BUF_MAX_SIZE) { 43 if (size > BUF_MAX_SIZE) {
46 dropbear_exit("buf->size too big"); 44 dropbear_exit("buf->size too big");
47 } 45 }
48 46
49 buf = (buffer*)m_malloc(sizeof(buffer)+size); 47 buf = (buffer*)m_malloc(sizeof(buffer)+size);
50 48 buf->data = (unsigned char*)buf + sizeof(buffer);
51 if (size > 0) {
52 buf->data = (unsigned char*)buf + sizeof(buffer);
53 } else {
54 buf->data = NULL;
55 }
56
57 buf->size = size; 49 buf->size = size;
58
59 return buf; 50 return buf;
60
61 } 51 }
62 52
63 /* free the buffer's data and the buffer itself */ 53 /* free the buffer's data and the buffer itself */
64 void buf_free(buffer* buf) { 54 void buf_free(buffer* buf) {
65
66 m_free(buf); 55 m_free(buf);
67 } 56 }
68 57
69 /* overwrite the contents of the buffer to clear it */ 58 /* overwrite the contents of the buffer to clear it */
70 void buf_burn(const buffer* buf) { 59 void buf_burn(const buffer* buf) {
71
72 m_burn(buf->data, buf->size); 60 m_burn(buf->data, buf->size);
73
74 } 61 }
75 62
76 /* resize a buffer, pos and len will be repositioned if required when 63 /* resize a buffer, pos and len will be repositioned if required when
77 * downsizing */ 64 * downsizing */
78 buffer* buf_resize(buffer *buf, unsigned int newsize) { 65 buffer* buf_resize(buffer *buf, unsigned int newsize) {
79
80 if (newsize > BUF_MAX_SIZE) { 66 if (newsize > BUF_MAX_SIZE) {
81 dropbear_exit("buf->size too big"); 67 dropbear_exit("buf->size too big");
82 } 68 }
83 69
84 buf = m_realloc(buf, sizeof(buffer)+newsize); 70 buf = m_realloc(buf, sizeof(buffer)+newsize);