tidy/CVE-2021-33391-pre.patch
2023-12-28 11:21:35 +08:00

214 lines
5.0 KiB
Diff

Origin: https://launchpadlibrarian.net/697070499/tidy-html5_2%3A5.6.0-11_2%3A5.6.0-11ubuntu0.20.04.1.diff.gz
Partial backport of:
From e56716f154f13b14fc5585146a85000fdd26d319 Mon Sep 17 00:00:00 2001
From: Jim Derry <balthisar@gmail.com>
Date: Wed, 28 Jul 2021 19:45:57 -0400
Subject: [PATCH] Improve internal documentation. Start general conversion to
eliminate and/or reduce recursion.
---
src/clean.c | 83 +-
src/lexer.c | 122 +-
src/lexer.h | 698 +++++---
src/parser.c | 4554 ++++++++++++++++++++++++++++++++++--------------
src/parser.h | 71 +-
src/tags.c | 4 +-
src/tags.h | 7 +-
src/tidy-int.h | 16 +-
src/tidylib.c | 2 +
9 files changed, 3873 insertions(+), 1684 deletions(-)
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -4425,6 +4425,106 @@ static Node *ParseDocTypeDecl(TidyDocImp
return NULL;
}
+
+/****************************************************************************//*
+ ** MARK: - Node Stack
+ ***************************************************************************/
+
+
+/**
+ * Create a new stack with a given starting capacity. If memory allocation
+ * fails, then the allocator will panic the program automatically.
+ */
+Stack* TY_(newStack)(TidyDocImpl *doc, uint capacity)
+{
+ Stack *stack = (Stack *)TidyAlloc(doc->allocator, sizeof(Stack));
+ stack->top = -1;
+ stack->capacity = capacity;
+ stack->firstNode = (Node **)TidyAlloc(doc->allocator, stack->capacity * sizeof(Node**));
+ stack->allocator = doc->allocator;
+ return stack;
+}
+
+
+/**
+ * Increase the stack size. This will be called automatically when the
+ * current stack is full. If memory allocation fails, then the allocator
+ * will panic the program automatically.
+ */
+void TY_(growStack)(Stack *stack)
+{
+ uint new_capacity = stack->capacity * 2;
+
+ Node **firstNode = (Node **)TidyAlloc(stack->allocator, new_capacity);
+
+ memcpy( firstNode, stack->firstNode, sizeof(Node**) * (stack->top + 1) );
+ TidyFree(stack->allocator, stack->firstNode);
+
+ stack->firstNode = firstNode;
+ stack->capacity = new_capacity;
+}
+
+
+/**
+ * Stack is full when top is equal to the last index.
+ */
+Bool TY_(stackFull)(Stack *stack)
+{
+ return stack->top == stack->capacity - 1;
+}
+
+
+/**
+ * Stack is empty when top is equal to -1
+ */
+Bool TY_(stackEmpty)(Stack *stack)
+{
+ return stack->top == -1;
+}
+
+
+/**
+ * Push an item to the stack.
+ */
+void TY_(push)(Stack *stack, Node *node)
+{
+ if (TY_(stackFull)(stack))
+ TY_(growStack)(stack);
+
+ if (node)
+ stack->firstNode[++stack->top] = node;
+}
+
+
+/**
+ * Pop an item from the stack.
+ */
+Node* TY_(pop)(Stack *stack)
+{
+ return TY_(stackEmpty)(stack) ? NULL : stack->firstNode[stack->top--];
+}
+
+
+/**
+ * Peek at the stack.
+ */
+FUNC_UNUSED Node* TY_(peek)(Stack *stack)
+{
+ return TY_(stackEmpty)(stack) ? NULL : stack->firstNode[stack->top--];
+}
+
+/**
+ * Frees the stack when done.
+ */
+void TY_(freeStack)(Stack *stack)
+{
+ TidyFree( stack->allocator, stack->firstNode );
+ stack->top = -1;
+ stack->capacity = 0;
+ stack->firstNode = NULL;
+ stack->allocator = NULL;
+}
+
/*
* local variables:
* mode: c
--- a/src/lexer.h
+++ b/src/lexer.h
@@ -594,6 +594,78 @@ Node* TY_(InsertedToken)( TidyDocImpl* d
Bool TY_(SwitchInline)( TidyDocImpl* doc, Node* element, Node* node );
Bool TY_(InlineDup1)( TidyDocImpl* doc, Node* node, Node* element );
+/** @}
+ * @name Generic stack of nodes.
+ * @{
+ */
+
+
+/**
+ * This typedef represents a stack of addresses to nodes. Tidy uses these to
+ * try to limit recursion by pushing nodes to a stack when possible instead
+ * of recursing.
+ */
+typedef struct _Stack {
+ int top; /**< Current top position. */
+ unsigned capacity; /**< Current capacity. Can be expanded. */
+ Node **firstNode; /** A pointer to the first pointer to a Node in an array of node addresses. */
+ TidyAllocator* allocator; /**< Tidy's allocator, used at instantiation and expanding. */
+} Stack;
+
+
+/**
+ * Create a new stack with a given starting capacity. If memory allocation
+ * fails, then the allocator will panic the program automatically.
+ */
+Stack* TY_(newStack)(TidyDocImpl *doc, uint capacity);
+
+
+/**
+ * Increase the stack size. This will be called automatically when the
+ * current stack is full. If memory allocation fails, then the allocator
+ * will panic the program automatically.
+ */
+void TY_(growStack)(Stack *stack);
+
+
+/**
+ * Stack is full when top is equal to the last index.
+ */
+Bool TY_(stackFull)(Stack *stack);
+
+
+/**
+ * Stack is empty when top is equal to -1
+ */
+Bool TY_(stackEmpty)(Stack *stack);
+
+
+/**
+ * Push an item to the stack.
+ */
+void TY_(push)(Stack *stack, Node *node);
+
+
+/**
+ * Pop an item from the stack.
+ */
+Node* TY_(pop)(Stack *stack);
+
+
+/**
+ * Peek at the stack.
+ */
+Node* TY_(peek)(Stack *stack);
+
+/**
+ * Frees the stack when done.
+ */
+void TY_(freeStack)(Stack *stack);
+
+
+/** @}
+ */
+
#ifdef __cplusplus
}
#endif