on master help sign in
Fix allocation of buffer
a74200cNiklas Larsson
rts/idris_buffer.c+2−3
@@ -3,7 +3,7 @@
3
3
4typedef struct {
4typedef struct {
5 int size;
5 int size;
6− uint8_t* data;
6+ uint8_t data[0];
7} Buffer;
7} Buffer;
8
8
9void* idris_newBuffer(int bytes) {
9void* idris_newBuffer(int bytes) {
@@ -13,7 +13,6 @@ void* idris_newBuffer(int bytes) {
13 }
13 }
14
14
15 buf->size = bytes;
15 buf->size = bytes;
16− buf->data = (uint8_t*)(buf+1);
17 memset(buf->data, 0, bytes);
16 memset(buf->data, 0, bytes);
18
17
19 return buf;
18 return buf;
@@ -25,7 +24,7 @@ void idris_copyBuffer(void* from, int start, int len,
25 Buffer* bto = to;
24 Buffer* bto = to;
26
25
27 if (loc >= 0 && loc+len <= bto->size) {
26 if (loc >= 0 && loc+len <= bto->size) {
28− memcpy((bto->data)+loc, (bfrom->data)+start, len);
27+ memcpy(bto->data + loc, bfrom->data + start, len);
29 }
28 }
30}
29}
31
30
test/buffer001/buffer001.idr+5
@@ -26,3 +26,8 @@ main = do Just buf <- newBuffer 40
26 closeFile h
26 closeFile h
27
27
28 printLn !(bufferData buf3)
28 printLn !(bufferData buf3)
29+
30+ -- Small buffer, tests issue #3828
31+ Just small <- newBuffer 3
32+ setByte small 0 0
33+ setByte small 2 1