diff --git a/src/dom/select.c b/src/dom/select.c index a0e5d7644..ff84cbee0 100644 --- a/src/dom/select.c +++ b/src/dom/select.c @@ -506,7 +506,7 @@ parse_dom_select(struct dom_select *select, struct dom_stack *stack, select->selector = select_node; } - if (!push_dom_node(stack, &select_node->node)) + if (push_dom_node(stack, &select_node->node) != DOM_STACK_CODE_OK) return DOM_ERR_INVALID_STATE; if (select_node->node.type != DOM_NODE_ELEMENT) @@ -1070,7 +1070,7 @@ select_dom_nodes(struct dom_select *select, struct dom_node *root) &dom_select_data_context_info); add_dom_stack_tracer(&select_data.stack, "select-match: "); - if (push_dom_node(&select_data.stack, &select->selector->node)) { + if (push_dom_node(&select_data.stack, &select->selector->node) == DOM_STACK_CODE_OK) { get_dom_stack_top(&select_data.stack)->immutable = 1; walk_dom_nodes(&stack, root); } diff --git a/src/dom/sgml/parser.c b/src/dom/sgml/parser.c index fce56a9e2..411fa883d 100644 --- a/src/dom/sgml/parser.c +++ b/src/dom/sgml/parser.c @@ -39,7 +39,10 @@ add_sgml_document(struct dom_stack *stack, struct dom_string *string) { struct dom_node *node = init_dom_node(DOM_NODE_DOCUMENT, string); - return node ? push_dom_node(stack, node) : NULL; + if (node && push_dom_node(stack, node) == DOM_STACK_CODE_OK) + return node; + + return NULL; } static inline struct dom_node * @@ -58,7 +61,7 @@ add_sgml_element(struct dom_stack *stack, struct dom_scanner_token *token) node_info = get_sgml_node_info(parser->info->elements, node); node->data.element.type = node_info->type; - if (!push_dom_node(stack, node)) + if (push_dom_node(stack, node) != DOM_STACK_CODE_OK) return NULL; state = get_dom_stack_top(stack); @@ -92,7 +95,7 @@ add_sgml_attribute(struct dom_stack *stack, if (valtoken && valtoken->type == SGML_TOKEN_STRING) node->data.attribute.quoted = 1; - if (!node || !push_dom_node(stack, node)) + if (!node || push_dom_node(stack, node) != DOM_STACK_CODE_OK) return; pop_dom_node(stack); @@ -119,7 +122,10 @@ add_sgml_proc_instruction(struct dom_stack *stack, struct dom_scanner_token *tar node->data.proc_instruction.type = DOM_PROC_INSTRUCTION; } - return push_dom_node(stack, node); + if (push_dom_node(stack, node) == DOM_STACK_CODE_OK) + return node; + + return NULL; } static inline void @@ -133,7 +139,7 @@ add_sgml_node(struct dom_stack *stack, enum dom_node_type type, struct dom_scann if (token->type == SGML_TOKEN_SPACE) node->data.text.only_space = 1; - if (push_dom_node(stack, node)) + if (push_dom_node(stack, node) == DOM_STACK_CODE_OK) pop_dom_node(stack); } @@ -403,7 +409,7 @@ parse_sgml(struct sgml_parser *parser, unsigned char *buf, size_t bufsize, } node = init_dom_node(DOM_NODE_TEXT, &source); - if (!node || !push_dom_node(&parser->parsing, node)) + if (!node || push_dom_node(&parser->parsing, node) != DOM_STACK_CODE_OK) return SGML_PARSER_CODE_MEM_ALLOC; pop_dom_node(&parser->parsing); diff --git a/src/dom/stack.c b/src/dom/stack.c index f41c1cb7a..ba5a8d629 100644 --- a/src/dom/stack.c +++ b/src/dom/stack.c @@ -154,7 +154,7 @@ call_dom_stack_callbacks(struct dom_stack *stack, struct dom_stack_state *state, } } -struct dom_node * +enum dom_stack_code push_dom_node(struct dom_stack *stack, struct dom_node *node) { struct dom_stack_state *state; @@ -164,13 +164,13 @@ push_dom_node(struct dom_stack *stack, struct dom_node *node) assert(0 < node->type && node->type < DOM_NODES); if (stack->depth > DOM_STACK_MAX_DEPTH) { - return NULL; + return DOM_STACK_CODE_ERROR_MAX_DEPTH; } state = realloc_dom_stack_states(&stack->states, stack->depth); if (!state) { done_dom_node(node); - return NULL; + return DOM_STACK_CODE_ERROR_MEM_ALLOC; } state += stack->depth; @@ -181,7 +181,7 @@ push_dom_node(struct dom_stack *stack, struct dom_node *node) if (context->info->object_size && !realloc_dom_stack_state_objects(context, stack->depth)) { done_dom_node(node); - return NULL; + return DOM_STACK_CODE_ERROR_MEM_ALLOC; } } @@ -193,7 +193,7 @@ push_dom_node(struct dom_stack *stack, struct dom_node *node) stack->depth++; call_dom_stack_callbacks(stack, state, DOM_STACK_PUSH); - return node; + return DOM_STACK_CODE_OK; } void @@ -349,7 +349,8 @@ walk_dom_nodes(struct dom_stack *stack, struct dom_node *root) if (!context) return; - push_dom_node(stack, root); + if (push_dom_node(stack, root) != DOM_STACK_CODE_OK) + return; while (!dom_stack_is_empty(stack)) { struct dom_stack_state *state = get_dom_stack_top(stack); diff --git a/src/dom/stack.h b/src/dom/stack.h index 153654e01..09c21229c 100644 --- a/src/dom/stack.h +++ b/src/dom/stack.h @@ -9,6 +9,16 @@ struct dom_stack; /* API Doc :: dom-stack */ +/** DOM stack code + * + * Codes used by the DOM stack to indicate states. + */ +enum dom_stack_code { + DOM_STACK_CODE_OK, /*: All is well */ + DOM_STACK_CODE_ERROR_MEM_ALLOC, /*: Memory allocation failure */ + DOM_STACK_CODE_ERROR_MAX_DEPTH, /*: Stack max depth reached */ +}; + /** DOM stack callback * * Used by contexts, for 'hooking' into the node traversing. */ @@ -230,7 +240,7 @@ void done_dom_stack_context(struct dom_stack *stack, struct dom_stack_context *c * * If an error occurs the node is released with ref:[done_dom_node] and NULL is * returned. Else the pushed node is returned. */ -struct dom_node *push_dom_node(struct dom_stack *stack, struct dom_node *node); +enum dom_stack_code push_dom_node(struct dom_stack *stack, struct dom_node *node); /** Pop the top stack state *