Skip to content

Commit a7f63ff

Browse files
ali-rantakariuranusjr
authored andcommitted
Avoid passing NULL to strdup()
Cherry-picked from ali-rantakari/peg-markdown-highlight@6d501919b30ca18980030df8 Fix #532.
1 parent eb79def commit a7f63ff

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

Dependency/peg-markdown-highlight/pmh_grammar.leg

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,8 @@ ReferenceLinkDouble = < s:Label Spnl !"[]" l:Label >
483483
pmh_realelement *reference = GET_REF(l->label);
484484
if (reference) {
485485
$$ = elem_s(pmh_LINK);
486-
$$->label = strdup(l->label);
487-
$$->address = strdup(reference->address);
486+
$$->label = strdup_or_null(l->label);
487+
$$->address = strdup_or_null(reference->address);
488488
} else
489489
$$ = NULL;
490490
FREE_LABEL(s);
@@ -496,8 +496,8 @@ ReferenceLinkSingle = < s:Label (Spnl "[]")? >
496496
pmh_realelement *reference = GET_REF(s->label);
497497
if (reference) {
498498
$$ = elem_s(pmh_LINK);
499-
$$->label = strdup(s->label);
500-
$$->address = strdup(reference->address);
499+
$$->label = strdup_or_null(s->label);
500+
$$->address = strdup_or_null(reference->address);
501501
} else
502502
$$ = NULL;
503503
FREE_LABEL(s);
@@ -507,7 +507,7 @@ ExplicitLink = < s:Label Spnl '(' Sp l:Source Spnl Title Sp ')' >
507507
{
508508
$$ = elem_s(pmh_LINK);
509509
if (l->address != NULL)
510-
$$->address = strdup(l->address);
510+
$$->address = strdup_or_null(l->address);
511511
FREE_LABEL(s);
512512
FREE_ADDRESS(l);
513513
}
@@ -550,8 +550,8 @@ Reference = < s:LocMarker
550550
NonindentSpace !"[]" l:Label ':' Spnl r:RefSrc RefTitle > BlankLine+
551551
{
552552
pmh_realelement *el = elem_s(pmh_REFERENCE);
553-
el->label = strdup(l->label);
554-
el->address = strdup(r->address);
553+
el->label = strdup_or_null(l->label);
554+
el->address = strdup_or_null(r->address);
555555
ADD(el);
556556
FREE_LABEL(l);
557557
FREE_ADDRESS(r);

Dependency/peg-markdown-highlight/pmh_parser_head.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
#endif
2626

2727

28+
char *strdup_or_null(char *s)
29+
{
30+
return (s == NULL) ? NULL : strdup(s);
31+
}
32+
2833

2934
// Alias strdup to _strdup on MSVC:
3035
#ifdef _MSC_VER
@@ -704,9 +709,9 @@ static pmh_realelement *mk_element(parser_data *p_data, pmh_element_type type,
704709
static pmh_realelement *copy_element(parser_data *p_data, pmh_realelement *elem)
705710
{
706711
pmh_realelement *result = mk_element(p_data, elem->type, elem->pos, elem->end);
707-
result->label = (elem->label == NULL) ? NULL : strdup(elem->label);
708-
result->text = (elem->text == NULL) ? NULL : strdup(elem->text);
709-
result->address = (elem->address == NULL) ? NULL : strdup(elem->address);
712+
result->label = strdup_or_null(elem->label);
713+
result->text = strdup_or_null(elem->text);
714+
result->address = strdup_or_null(elem->address);
710715
return result;
711716
}
712717

@@ -716,7 +721,7 @@ static pmh_realelement *mk_etext(parser_data *p_data, char *string)
716721
pmh_realelement *result;
717722
assert(string != NULL);
718723
result = mk_element(p_data, pmh_EXTRA_TEXT, 0,0);
719-
result->text = strdup(string);
724+
result->text = strdup_or_null(string);
720725
return result;
721726
}
722727

0 commit comments

Comments
 (0)