Skip to content

Commit

Permalink
Avoid passing NULL to strdup()
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-rantakari authored and uranusjr committed Nov 5, 2016
1 parent eb79def commit a7f63ff
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
14 changes: 7 additions & 7 deletions Dependency/peg-markdown-highlight/pmh_grammar.leg
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 9 additions & 4 deletions Dependency/peg-markdown-highlight/pmh_parser_head.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down

0 comments on commit a7f63ff

Please sign in to comment.