From a7f63ffdc617c9bb2e18200ec450d473d30a0ce9 Mon Sep 17 00:00:00 2001 From: Ali Rantakari Date: Sun, 6 Nov 2016 05:10:22 +0800 Subject: [PATCH] Avoid passing NULL to strdup() Cherry-picked from ali-rantakari/peg-markdown-highlight@6d501919b30ca18980030df8 Fix #532. --- Dependency/peg-markdown-highlight/pmh_grammar.leg | 14 +++++++------- .../peg-markdown-highlight/pmh_parser_head.c | 13 +++++++++---- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/Dependency/peg-markdown-highlight/pmh_grammar.leg b/Dependency/peg-markdown-highlight/pmh_grammar.leg index 13a3273a..a4731a50 100644 --- a/Dependency/peg-markdown-highlight/pmh_grammar.leg +++ b/Dependency/peg-markdown-highlight/pmh_grammar.leg @@ -483,8 +483,8 @@ ReferenceLinkDouble = < s:Label Spnl !"[]" l:Label > pmh_realelement *reference = GET_REF(l->label); if (reference) { $$ = elem_s(pmh_LINK); - $$->label = strdup(l->label); - $$->address = strdup(reference->address); + $$->label = strdup_or_null(l->label); + $$->address = strdup_or_null(reference->address); } else $$ = NULL; FREE_LABEL(s); @@ -496,8 +496,8 @@ ReferenceLinkSingle = < s:Label (Spnl "[]")? > pmh_realelement *reference = GET_REF(s->label); if (reference) { $$ = elem_s(pmh_LINK); - $$->label = strdup(s->label); - $$->address = strdup(reference->address); + $$->label = strdup_or_null(s->label); + $$->address = strdup_or_null(reference->address); } else $$ = NULL; FREE_LABEL(s); @@ -507,7 +507,7 @@ ExplicitLink = < s:Label Spnl '(' Sp l:Source Spnl Title Sp ')' > { $$ = elem_s(pmh_LINK); if (l->address != NULL) - $$->address = strdup(l->address); + $$->address = strdup_or_null(l->address); FREE_LABEL(s); FREE_ADDRESS(l); } @@ -550,8 +550,8 @@ Reference = < s:LocMarker NonindentSpace !"[]" l:Label ':' Spnl r:RefSrc RefTitle > BlankLine+ { pmh_realelement *el = elem_s(pmh_REFERENCE); - el->label = strdup(l->label); - el->address = strdup(r->address); + el->label = strdup_or_null(l->label); + el->address = strdup_or_null(r->address); ADD(el); FREE_LABEL(l); FREE_ADDRESS(r); diff --git a/Dependency/peg-markdown-highlight/pmh_parser_head.c b/Dependency/peg-markdown-highlight/pmh_parser_head.c index f5ee983b..fefc8212 100644 --- a/Dependency/peg-markdown-highlight/pmh_parser_head.c +++ b/Dependency/peg-markdown-highlight/pmh_parser_head.c @@ -25,6 +25,11 @@ #endif +char *strdup_or_null(char *s) +{ + return (s == NULL) ? NULL : strdup(s); +} + // Alias strdup to _strdup on MSVC: #ifdef _MSC_VER @@ -704,9 +709,9 @@ static pmh_realelement *mk_element(parser_data *p_data, pmh_element_type type, static pmh_realelement *copy_element(parser_data *p_data, pmh_realelement *elem) { pmh_realelement *result = mk_element(p_data, elem->type, elem->pos, elem->end); - result->label = (elem->label == NULL) ? NULL : strdup(elem->label); - result->text = (elem->text == NULL) ? NULL : strdup(elem->text); - result->address = (elem->address == NULL) ? NULL : strdup(elem->address); + result->label = strdup_or_null(elem->label); + result->text = strdup_or_null(elem->text); + result->address = strdup_or_null(elem->address); return result; } @@ -716,7 +721,7 @@ static pmh_realelement *mk_etext(parser_data *p_data, char *string) pmh_realelement *result; assert(string != NULL); result = mk_element(p_data, pmh_EXTRA_TEXT, 0,0); - result->text = strdup(string); + result->text = strdup_or_null(string); return result; }