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, ...) {...@@ -25,6 +25,7 @@ Buf *buf_sprintf(const char *format, ...) {
25}25}
2626
27void buf_appendf(Buf *buf, const char *format, ...) {27void buf_appendf(Buf *buf, const char *format, ...) {
28 assert(buf->list.length);
28 va_list ap, ap2;29 va_list ap, ap2;
29 va_start(ap, format);30 va_start(ap, format);
30 va_copy(ap2, ap);31 va_copy(ap2, ap);
src/buffer.hpp+15
...@@ -15,6 +15,8 @@...@@ -15,6 +15,8 @@
1515
16#define BUF_INIT {{0}}16#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.
18struct Buf {20struct Buf {
19 ZigList<char> list;21 ZigList<char> list;
20};22};
...@@ -23,10 +25,12 @@ Buf *buf_sprintf(const char *format, ...)...@@ -23,10 +25,12 @@ Buf *buf_sprintf(const char *format, ...)
23 __attribute__ ((format (printf, 1, 2)));25 __attribute__ ((format (printf, 1, 2)));
2426
25static inline int buf_len(Buf *buf) {27static inline int buf_len(Buf *buf) {
28 assert(buf->list.length);
26 return buf->list.length - 1;29 return buf->list.length - 1;
27}30}
2831
29static inline char *buf_ptr(Buf *buf) {32static inline char *buf_ptr(Buf *buf) {
33 assert(buf->list.length);
30 return buf->list.items;34 return buf->list.items;
31}35}
3236
...@@ -76,6 +80,7 @@ static inline Buf *buf_create_from_str(const char *str) {...@@ -76,6 +80,7 @@ static inline Buf *buf_create_from_str(const char *str) {
76}80}
7781
78static inline Buf *buf_slice(Buf *in_buf, int start, int end) {82static inline Buf *buf_slice(Buf *in_buf, int start, int end) {
83 assert(in_buf->list.length);
79 assert(start >= 0);84 assert(start >= 0);
80 assert(end >= 0);85 assert(end >= 0);
81 assert(start < buf_len(in_buf));86 assert(start < buf_len(in_buf));
...@@ -88,6 +93,7 @@ static inline Buf *buf_slice(Buf *in_buf, int start, int end) {...@@ -88,6 +93,7 @@ static inline Buf *buf_slice(Buf *in_buf, int start, int end) {
88}93}
8994
90static inline void buf_append_mem(Buf *buf, const char *mem, int mem_len) {95static inline void buf_append_mem(Buf *buf, const char *mem, int mem_len) {
96 assert(buf->list.length);
91 assert(mem_len >= 0);97 assert(mem_len >= 0);
92 int old_len = buf_len(buf);98 int old_len = buf_len(buf);
93 buf_resize(buf, old_len + mem_len);99 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) {...@@ -96,14 +102,17 @@ static inline void buf_append_mem(Buf *buf, const char *mem, int mem_len) {
96}102}
97103
98static inline void buf_append_str(Buf *buf, const char *str) {104static inline void buf_append_str(Buf *buf, const char *str) {
105 assert(buf->list.length);
99 buf_append_mem(buf, str, strlen(str));106 buf_append_mem(buf, str, strlen(str));
100}107}
101108
102static inline void buf_append_buf(Buf *buf, Buf *append_buf) {109static inline void buf_append_buf(Buf *buf, Buf *append_buf) {
110 assert(buf->list.length);
103 buf_append_mem(buf, buf_ptr(append_buf), buf_len(append_buf));111 buf_append_mem(buf, buf_ptr(append_buf), buf_len(append_buf));
104}112}
105113
106static inline void buf_append_char(Buf *buf, uint8_t c) {114static inline void buf_append_char(Buf *buf, uint8_t c) {
115 assert(buf->list.length);
107 buf_append_mem(buf, (const char *)&c, 1);116 buf_append_mem(buf, (const char *)&c, 1);
108}117}
109118
...@@ -111,20 +120,25 @@ void buf_appendf(Buf *buf, const char *format, ...)...@@ -111,20 +120,25 @@ void buf_appendf(Buf *buf, const char *format, ...)
111 __attribute__ ((format (printf, 2, 3)));120 __attribute__ ((format (printf, 2, 3)));
112121
113static inline bool buf_eql_mem(Buf *buf, const char *mem, int mem_len) {122static inline bool buf_eql_mem(Buf *buf, const char *mem, int mem_len) {
123 assert(buf->list.length);
114 if (buf_len(buf) != mem_len)124 if (buf_len(buf) != mem_len)
115 return false;125 return false;
116 return memcmp(buf_ptr(buf), mem, mem_len) == 0;126 return memcmp(buf_ptr(buf), mem, mem_len) == 0;
117}127}
118128
119static inline bool buf_eql_str(Buf *buf, const char *str) {129static inline bool buf_eql_str(Buf *buf, const char *str) {
130 assert(buf->list.length);
120 return buf_eql_mem(buf, str, strlen(str));131 return buf_eql_mem(buf, str, strlen(str));
121}132}
122133
123static inline bool buf_eql_buf(Buf *buf, Buf *other) {134static inline bool buf_eql_buf(Buf *buf, Buf *other) {
135 assert(buf->list.length);
124 return buf_eql_mem(buf, buf_ptr(other), buf_len(other));136 return buf_eql_mem(buf, buf_ptr(other), buf_len(other));
125}137}
126138
127static inline void buf_splice_buf(Buf *buf, int start, int end, Buf *other) {139static inline void buf_splice_buf(Buf *buf, int start, int end, Buf *other) {
140 assert(buf->list.length);
141
128 if (start != end)142 if (start != end)
129 zig_panic("TODO buf_splice_buf");143 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) {...@@ -135,6 +149,7 @@ static inline void buf_splice_buf(Buf *buf, int start, int end, Buf *other) {
135}149}
136150
137static inline uint32_t buf_hash(Buf *buf) {151static inline uint32_t buf_hash(Buf *buf) {
152 assert(buf->list.length);
138 // FNV 32-bit hash153 // FNV 32-bit hash
139 uint32_t h = 2166136261;154 uint32_t h = 2166136261;
140 for (int i = 0; i < buf_len(buf); i += 1) {155 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) {...@@ -179,6 +179,7 @@ static void resolve_type_and_recurse(CodeGen *g, AstNode *node) {
179 TypeTableEntry *entry = allocate<TypeTableEntry>(1);179 TypeTableEntry *entry = allocate<TypeTableEntry>(1);
180 entry->id = TypeIdPointer;180 entry->id = TypeIdPointer;
181 entry->type_ref = LLVMPointerType(child_type_node->entry->type_ref, 0);181 entry->type_ref = LLVMPointerType(child_type_node->entry->type_ref, 0);
182 buf_resize(&entry->name, 0);
182 buf_appendf(&entry->name, "*%s %s", const_or_mut_str, buf_ptr(&child_type_node->entry->name));183 buf_appendf(&entry->name, "*%s %s", const_or_mut_str, buf_ptr(&child_type_node->entry->name));
183 entry->di_type = g->dbuilder->createPointerType(child_type_node->entry->di_type,184 entry->di_type = g->dbuilder->createPointerType(child_type_node->entry->di_type,
184 g->pointer_size_bytes * 8, g->pointer_size_bytes * 8, buf_ptr(&entry->name));185 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) {...@@ -181,8 +181,7 @@ static AstNode *ast_create_node(NodeType type, Token *first_token) {
181}181}
182182
183static AstNode *ast_create_node_with_node(NodeType type, AstNode *other_node) {183static AstNode *ast_create_node_with_node(NodeType type, AstNode *other_node) {
184 AstNode *node = allocate<AstNode>(1);184 AstNode *node = ast_create_node_no_line_info(type);
185 node->type = type;
186 node->line = other_node->line;185 node->line = other_node->line;
187 node->column = other_node->column;186 node->column = other_node->column;
188 return node;187 return node;
...@@ -202,8 +201,10 @@ static void ast_buf_from_token(ParseContext *pc, Token *token, Buf *buf) {...@@ -202,8 +201,10 @@ static void ast_buf_from_token(ParseContext *pc, Token *token, Buf *buf) {
202static void parse_string_literal(ParseContext *pc, Token *token, Buf *buf) {201static void parse_string_literal(ParseContext *pc, Token *token, Buf *buf) {
203 // skip the double quotes at beginning and end202 // skip the double quotes at beginning and end
204 // convert escape sequences203 // convert escape sequences
204
205 buf_resize(buf, 0);
205 bool escape = false;206 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) {
207 uint8_t c = *((uint8_t*)buf_ptr(pc->buf) + i);208 uint8_t c = *((uint8_t*)buf_ptr(pc->buf) + i);
208 if (escape) {209 if (escape) {
209 switch (c) {210 switch (c) {