on master help sign in
Fix allocation of buffer

Use a zero-length array to represent the memory allocated after the size. Remove spurious assignment. Fixes #3828

commita74200c0007635c767216b72f42780584357945f authorNiklas Larsson <niklas@mm.st> dateWed 24 May 2017, 10:23 (9 years ago) parentd176fb3
2 files changed+7−3
rts/idris_buffer.c+2−3 test/buffer001/buffer001.idr+5
rts/idris_buffer.c+2−3
@@ -3,7 +3,7 @@
33
44typedef struct {
55 int size;
6− uint8_t* data;
6+ uint8_t data[0];
77} Buffer;
88
99void* idris_newBuffer(int bytes) {
@@ -13,7 +13,6 @@ void* idris_newBuffer(int bytes) {
1313 }
1414
1515 buf->size = bytes;
16− buf->data = (uint8_t*)(buf+1);
1716 memset(buf->data, 0, bytes);
1817
1918 return buf;
@@ -25,7 +24,7 @@ void idris_copyBuffer(void* from, int start, int len,
2524 Buffer* bto = to;
2625
2726 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);
2928 }
3029}
3130
test/buffer001/buffer001.idr+5
@@ -26,3 +26,8 @@ main = do Just buf <- newBuffer 40
2626 closeFile h
2727
2828 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