From 5f98ccd606310bf6b96e5acd8c5048e359f44908 Mon Sep 17 00:00:00 2001 From: Jim Derry Date: Fri, 30 Jul 2021 18:32:34 -0400 Subject: [PATCH] Static analyzer fixes. --- src/fileio.c | 3 ++- src/lexer.c | 2 +- src/message.c | 12 ++++++------ src/messageobj.c | 1 - src/parser.c | 11 ++++++----- src/pprint.c | 2 -- src/streamio.c | 2 -- 7 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/fileio.c b/src/fileio.c index 61048c0..990916c 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -76,7 +76,8 @@ void TY_(freeFileSource)( TidyInputSource* inp, Bool closeIt ) if ( closeIt && fin && fin->fp ) fclose( fin->fp ); tidyBufFree( &fin->unget ); - TidyFree( fin->unget.allocator, fin ); + if (fin) + TidyFree( fin->unget.allocator, fin ); } void TIDY_CALL TY_(filesink_putByte)( void* sinkData, byte bv ) diff --git a/src/lexer.c b/src/lexer.c index fa8d6fb..b0afccd 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -4477,7 +4477,7 @@ void TY_(growStack)(Stack *stack) { uint new_capacity = stack->capacity * 2; - Node **firstNode = (Node **)TidyAlloc(stack->allocator, new_capacity); + Node **firstNode = (Node **)TidyAlloc(stack->allocator, new_capacity * sizeof(Node**)); memcpy( firstNode, stack->firstNode, sizeof(Node**) * (stack->top + 1) ); TidyFree(stack->allocator, stack->firstNode); diff --git a/src/message.c b/src/message.c index 90f74e0..2f8cf73 100644 --- a/src/message.c +++ b/src/message.c @@ -792,7 +792,7 @@ TidyMessageImpl *formatStandard(TidyDocImpl* doc, Node *element, Node *node, uin case TAG_NOT_ALLOWED_IN: /* Can we use `rpt` here? No; `element` has a value in every case. */ - return TY_(tidyMessageCreateWithNode)(doc, node, code, level, nodedesc, element->element ); + return TY_(tidyMessageCreateWithNode)(doc, node, code, level, nodedesc, element ? element->element : NULL ); case INSERTING_TAG: case MISSING_STARTTAG: @@ -804,7 +804,7 @@ TidyMessageImpl *formatStandard(TidyDocImpl* doc, Node *element, Node *node, uin case UNEXPECTED_ENDTAG_IN: /* Can we use `rpt` here? No; `element` has a value in every case. */ - return TY_(tidyMessageCreateWithNode)(doc, node, code, level, node->element, element->element ); + return TY_(tidyMessageCreateWithNode)(doc, node, code, level, node->element, element ? element->element : NULL ); case BAD_CDATA_CONTENT: case CONTENT_AFTER_BODY: @@ -848,16 +848,16 @@ TidyMessageImpl *formatStandard(TidyDocImpl* doc, Node *element, Node *node, uin case MISSING_ENDTAG_FOR: case MISSING_ENDTAG_OPTIONAL: case PREVIOUS_LOCATION: - return TY_(tidyMessageCreateWithNode)(doc, rpt, code, level, element->element ); + return TY_(tidyMessageCreateWithNode)(doc, rpt, code, level, element? element->element : NULL ); case MISSING_ENDTAG_BEFORE: - return TY_(tidyMessageCreateWithNode)(doc, rpt, code, level, element->element, nodedesc ); + return TY_(tidyMessageCreateWithNode)(doc, rpt, code, level, element? element->element : NULL, nodedesc ); case COERCE_TO_ENDTAG: case NON_MATCHING_ENDTAG: return TY_(tidyMessageCreateWithNode)(doc, rpt, code, level, node->element, node->element ); case TOO_MANY_ELEMENTS_IN: - return TY_(tidyMessageCreateWithNode)(doc, rpt, code, level, node->element, element->element); + return TY_(tidyMessageCreateWithNode)(doc, rpt, code, level, node->element, element ? element->element : NULL); } @@ -1504,7 +1504,7 @@ uint TY_(getNextErrorCode)( TidyIterator* iter ) } *iter = (TidyIterator)( itemIndex <= tidyErrorCodeListSize() ? itemIndex : (size_t)0 ); - return item->value; + return item ? item->value : 0; } diff --git a/src/messageobj.c b/src/messageobj.c index 1b986b7..98b83e8 100644 --- a/src/messageobj.c +++ b/src/messageobj.c @@ -203,7 +203,6 @@ static TidyMessageImpl *tidyMessageCreateInitV( TidyDocImpl *doc, i = TY_(tmbstrlen)(result->messageOutput); if (i < sizeMessageBuf) TY_(tmbsnprintf)(result->messageOutput + i, sizeMessageBuf - i, " (%s)", pc ); - i = 0; } result->allowMessage = yes; diff --git a/src/parser.c b/src/parser.c index 2a4fcb9..5d1e866 100644 --- a/src/parser.c +++ b/src/parser.c @@ -2345,7 +2345,7 @@ void* TY_(oldParseDefList)(TidyDocImpl* doc, Node *list, GetTokenMode mode) * unless node has been blown away because the * center was empty, as above. */ - if (parent->last == node) + if (parent && parent->last == node) { list = TY_(InferredTag)(doc, TidyTag_DL); TY_(InsertNodeAfterElement)(node, list); @@ -4225,14 +4225,14 @@ void* TY_(oldParseFrameSet)(TidyDocImpl* doc, Node *frameset, GetTokenMode ARG_U TY_(Report)(doc, frameset, node, INSERTING_TAG); } - if (node->type == StartTag && (node->tag->model & CM_FRAMES)) + if (node->type == StartTag && (node->tag && node->tag->model & CM_FRAMES)) { TY_(InsertNodeAtEnd)(frameset, node); lexer->excludeBlocks = no; ParseTag(doc, node, MixedContent); continue; } - else if (node->type == StartEndTag && (node->tag->model & CM_FRAMES)) + else if (node->type == StartEndTag && (node->tag && node->tag->model & CM_FRAMES)) { TY_(InsertNodeAtEnd)(frameset, node); continue; @@ -4345,7 +4345,7 @@ Node* TY_(ParseHTML)( TidyDocImpl *doc, Node *html, GetTokenMode mode, Bool popS } /* We did not expect to find an html closing tag here! */ - if (node->tag == html->tag && node->type == EndTag) + if (html && (node->tag == html->tag) && (node->type == EndTag)) { TY_(Report)(doc, html, node, DISCARDING_UNEXPECTED); TY_(FreeNode)( doc, node); @@ -5382,7 +5382,8 @@ Node* TY_(ParseNamespace)( TidyDocImpl* doc, Node *basenode, GetTokenMode mode, if (outside == no) { /* EndTag for a node within the basenode subtree. Roll on... */ - n->closed = yes; + if (n) + n->closed = yes; TY_(FreeNode)(doc, node); node = n; diff --git a/src/pprint.c b/src/pprint.c index 5522981..c433db3 100644 --- a/src/pprint.c +++ b/src/pprint.c @@ -1190,8 +1190,6 @@ static void PPrintAttribute( TidyDocImpl* doc, uint indent, indent += xtra; PCondFlushLineSmart( doc, indent ); } - else - indAttrs = no; } CheckWrapIndent( doc, indent ); diff --git a/src/streamio.c b/src/streamio.c index 07f05c7..96a5c7c 100644 --- a/src/streamio.c +++ b/src/streamio.c @@ -590,7 +590,6 @@ void TY_(WriteChar)( uint c, StreamOut* out ) if ( !TY_(IsValidUTF16FromUCS4)(c) ) { /* invalid UTF-16 value */ - c = 0; numChars = 0; } else if ( TY_(IsCombinedChar)(c) ) @@ -599,7 +598,6 @@ void TY_(WriteChar)( uint c, StreamOut* out ) numChars = 2; if ( !TY_(SplitSurrogatePair)(c, &theChars[0], &theChars[1]) ) { - c = 0; numChars = 0; } }