More memory cleanup and safety

This commit is contained in:
Wilson Lin 2018-08-08 10:41:39 +12:00
parent 78b4ccf6a8
commit e2e12f408b
6 changed files with 31 additions and 4 deletions

View File

@ -62,4 +62,12 @@ hb_bufferlist_t hb_bufferlist_create_from_split(hb_char_t *source, hb_char_t del
return parts;
}
void hb_bufferlist_destroy_from_split(hb_bufferlist_t list)
{
for (size_t i = 0; i < list->length; i++) {
hbu_buffer_destroy(hb_bufferlist_get(list, i));
}
hb_bufferlist_destroy(list);
}
#endif // _HDR_HYPERBUILD_DATASTRUCTURE_LIST_BUFFER

View File

@ -71,10 +71,12 @@ static nh_set_str_t _parse_list_of_tags(char *argv) {
if (!hbr_tags_check(part_c)) {
hbe_fatal(HBE_CLI_INVALID_TAG, "%s is not a valid tag and was provided as part of an argument's value", part_c);
}
nh_set_str_add(set, (char *) part_c);
nh_set_str_add(set, (char *) hbu_buffer_underlying_copy(part));
}
}
hb_bufferlist_destroy_from_split(list);
return set;
}
@ -106,6 +108,8 @@ static void _parse_and_add_errors_to_suppress(nh_set_int32_t suppressed_errors,
hbe_fatal(HBE_CLI_INVALID_SUPPRESSABLE_ERROR, "Unrecognised suppressable error `%s`", hbu_buffer_underlying(part));
}
}
hb_bufferlist_destroy_from_split(list);
}
int main(int argc, char **argv) {

View File

@ -44,6 +44,7 @@ static void _hbs_entity_write_literal(hbu_pipe_t pipe, int type, hbu_buffer_t en
static void _hbs_entity_handle_error(hbs_options_t so, hbu_pipe_t pipe, int type, hbu_buffer_t entity_raw, int consumed_semicolon, hbe_errcode_t errcode, const char *reason) {
if (hbs_options_supressed_error(so, errcode)) {
_hbs_entity_write_literal(pipe, type, entity_raw, consumed_semicolon);
hbu_buffer_destroy(entity_raw);
return;
}
@ -55,6 +56,7 @@ void hbs_entity(hbs_options_t so, hbu_pipe_t pipe) {
hb_char_t c = hbu_pipe_peek(pipe);
// _hbs_entity_handle_error will free this in case of error
hbu_buffer_t entity_raw = hbu_buffer_create_size(HBS_ENTITY_MAX_ENTITY_LENGTH + 1);
int type = -1;

View File

@ -40,6 +40,7 @@ void hbsh_attr(hbs_options_t so, hbu_pipe_t pipe) {
so->trim_class_attr;
hbu_buffer_destroy(name);
name = NULL;
if (hbu_pipe_accept_if(pipe, '=')) {
if (hbr_attrvalquote_check(hbu_pipe_peek(pipe))) {

View File

@ -30,6 +30,7 @@ void hbs_tag(hbs_options_t so, hbu_pipe_t pipe, hb_char_t *parent) {
hbu_pipe_require(pipe, '<');
hbu_buffer_t opening_name = hbsh_tagname(so, pipe);
while (1) {
hbu_pipe_accept_while_predicate(pipe, &hbr_whitespace_check);
@ -40,7 +41,6 @@ void hbs_tag(hbs_options_t so, hbu_pipe_t pipe, hb_char_t *parent) {
if (hbu_pipe_accept_if_matches(pipe, "/>")) {
if (!hbs_options_supressed_error(so, HBE_PARSE_SELF_CLOSING_TAG)) {
hbu_pipe_error(pipe, HBE_PARSE_SELF_CLOSING_TAG, "Self-closing tag");
// Unreachable
}
self_closing = 1;
break;
@ -65,7 +65,7 @@ void hbs_tag(hbs_options_t so, hbu_pipe_t pipe, hb_char_t *parent) {
// Self-closing or void tag
if (self_closing || hbr_voidtags_check(tag_name)) {
return;
goto cleanup;
}
if (hbu_buffer_compare_lit(opening_name, "script") == 0) {
@ -89,7 +89,12 @@ void hbs_tag(hbs_options_t so, hbu_pipe_t pipe, hb_char_t *parent) {
hbu_pipe_error(pipe, HBE_PARSE_UNCLOSED_TAG, "Tag not closed");
}
hbu_buffer_destroy(opening_name);
goto cleanup;
cleanup:
hbu_buffer_destroy(opening_name);
opening_name = NULL;
return;
}
#endif // _HDR_HYPERBUILD_STREAM_TAG

View File

@ -40,6 +40,13 @@
return buf->data; \
} \
\
elem_type *name##_underlying_copy(name##_t buf) \
{ \
elem_type *copy = hbu_mem_calloc(buf->length + 1, elem_size); \
memcpy(copy, buf->data, buf->length *elem_size); \
return copy; \
} \
\
int name##_valid_index(name##_t buf, size_t idx) \
{ \
return idx < buf->length; \