authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-11-25 15:17:19-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-11-25 15:17:19-07:00
loga600df073a9d1d1ebc166fb02582836f597a7f8f
treec3f2762b3fb67b71fb58936e06d93ca3f63d082b
parent0b59afec56ab62b58329b17581eaf166eb28e2c2

fix invalid memory write


4 files changed, 21 insertions(+), 3 deletions(-)

src/buffer.cpp+1
......@@ -25,6 +25,7 @@ Buf *buf_sprintf(const char *format, ...) {
2525}
2626
2727void buf_appendf(Buf *buf, const char *format, ...) {
28 assert(buf->list.length);
2829 va_list ap, ap2;
2930 va_start(ap, format);
3031 va_copy(ap2, ap);
src/buffer.hpp+15
......@@ -15,6 +15,8 @@
1515
1616#define BUF_INIT {{0}}
1717
18// Note, you must call one of the alloc, init, or resize functions to have an
19// initialized buffer. The assertions should help with this.
1820struct Buf {
1921 ZigList<char> list;
2022};
......@@ -23,10 +25,12 @@ Buf *buf_sprintf(const char *format, ...)
2325 __attribute__ ((format (printf, 1, 2)));
2426
2527static inline int buf_len(Buf *buf) {
28 assert(buf->list.length);
2629 return buf->list.length - 1;
2730}
2831
2932static inline char *buf_ptr(Buf *buf) {
33 assert(buf->list.length);
3034 return buf->list.items;
3135}
3236
......@@ -76,6 +80,7 @@ static inline Buf *buf_create_from_str(const char *str) {
7680}
7781
7882static inline Buf *buf_slice(Buf *in_buf, int start, int end) {
83 assert(in_buf->list.length);
7984 assert(start >= 0);
8085 assert(end >= 0);
8186 assert(start < buf_len(in_buf));
......@@ -88,6 +93,7 @@ static inline Buf *buf_slice(Buf *in_buf, int start, int end) {
8893}
8994
9095static inline void buf_append_mem(Buf *buf, const char *mem, int mem_len) {
96 assert(buf->list.length);
9197 assert(mem_len >= 0);
9298 int old_len = buf_len(buf);
9399 buf_resize(buf, old_len + mem_len);
......@@ -96,14 +102,17 @@ static inline void buf_append_mem(Buf *buf, const char *mem, int mem_len) {
96102}
97103
98104static inline void buf_append_str(Buf *buf, const char *str) {
105 assert(buf->list.length);
99106 buf_append_mem(buf, str, strlen(str));
100107}
101108
102109static inline void buf_append_buf(Buf *buf, Buf *append_buf) {
110 assert(buf->list.length);
103111 buf_append_mem(buf, buf_ptr(append_buf), buf_len(append_buf));
104112}
105113
106114static inline void buf_append_char(Buf *buf, uint8_t c) {
115 assert(buf->list.length);
107116 buf_append_mem(buf, (const char *)&c, 1);
108117}
109118
......@@ -111,20 +120,25 @@ void buf_appendf(Buf *buf, const char *format, ...)
111120 __attribute__ ((format (printf, 2, 3)));
112121
113122static inline bool buf_eql_mem(Buf *buf, const char *mem, int mem_len) {
123 assert(buf->list.length);
114124 if (buf_len(buf) != mem_len)
115125 return false;
116126 return memcmp(buf_ptr(buf), mem, mem_len) == 0;
117127}
118128
119129static inline bool buf_eql_str(Buf *buf, const char *str) {
130 assert(buf->list.length);
120131 return buf_eql_mem(buf, str, strlen(str));
121132}
122133
123134static inline bool buf_eql_buf(Buf *buf, Buf *other) {
135 assert(buf->list.length);
124136 return buf_eql_mem(buf, buf_ptr(other), buf_len(other));
125137}
126138
127139static inline void buf_splice_buf(Buf *buf, int start, int end, Buf *other) {
140 assert(buf->list.length);
141
128142 if (start != end)
129143 zig_panic("TODO buf_splice_buf");
130144
......@@ -135,6 +149,7 @@ static inline void buf_splice_buf(Buf *buf, int start, int end, Buf *other) {
135149}
136150
137151static inline uint32_t buf_hash(Buf *buf) {
152 assert(buf->list.length);
138153 // FNV 32-bit hash
139154 uint32_t h = 2166136261;
140155 for (int i = 0; i < buf_len(buf); i += 1) {
src/codegen.cpp+1
......@@ -179,6 +179,7 @@ static void resolve_type_and_recurse(CodeGen *g, AstNode *node) {
179179 TypeTableEntry *entry = allocate<TypeTableEntry>(1);
180180 entry->id = TypeIdPointer;
181181 entry->type_ref = LLVMPointerType(child_type_node->entry->type_ref, 0);
182 buf_resize(&entry->name, 0);
182183 buf_appendf(&entry->name, "*%s %s", const_or_mut_str, buf_ptr(&child_type_node->entry->name));
183184 entry->di_type = g->dbuilder->createPointerType(child_type_node->entry->di_type,
184185 g->pointer_size_bytes * 8, g->pointer_size_bytes * 8, buf_ptr(&entry->name));
src/parser.cpp+4-3
......@@ -181,8 +181,7 @@ static AstNode *ast_create_node(NodeType type, Token *first_token) {
181181}
182182
183183static AstNode *ast_create_node_with_node(NodeType type, AstNode *other_node) {
184 AstNode *node = allocate<AstNode>(1);
185 node->type = type;
184 AstNode *node = ast_create_node_no_line_info(type);
186185 node->line = other_node->line;
187186 node->column = other_node->column;
188187 return node;
......@@ -202,8 +201,10 @@ static void ast_buf_from_token(ParseContext *pc, Token *token, Buf *buf) {
202201static void parse_string_literal(ParseContext *pc, Token *token, Buf *buf) {
203202 // skip the double quotes at beginning and end
204203 // convert escape sequences
204
205 buf_resize(buf, 0);
205206 bool escape = false;
206 for (int i = token->start_pos; i < token->end_pos - 1; i += 1) {
207 for (int i = token->start_pos + 1; i < token->end_pos - 1; i += 1) {
207208 uint8_t c = *((uint8_t*)buf_ptr(pc->buf) + i);
208209 if (escape) {
209210 switch (c) {