From: Fletcher T. Penney Date: Tue, 4 Jul 2017 12:58:57 +0000 (-0400) Subject: UPDATE: Use custom strdup() replacement X-Git-Tag: 6.1.0^2~8 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dfff8c116559cda287c199ba47a3c0e129c760a7;p=multimarkdown UPDATE: Use custom strdup() replacement --- diff --git a/Sources/libMultiMarkdown/epub.c b/Sources/libMultiMarkdown/epub.c index 0452b95..fd5d555 100644 --- a/Sources/libMultiMarkdown/epub.c +++ b/Sources/libMultiMarkdown/epub.c @@ -79,8 +79,20 @@ #define print_localized(x) mmd_print_localized_char_html(out, x, scratch) +/// strdup() not available on all platforms +static char * my_strdup(const char * source) { + char * result = malloc(strlen(source) + 1); + + if (result) { + strcpy(result, source); + } + + return result; +} + + char * epub_mimetype(void) { - return strdup("application/epub+zip"); + return my_strdup("application/epub+zip"); } diff --git a/Sources/libMultiMarkdown/html.c b/Sources/libMultiMarkdown/html.c index 87a3b4c..ea7edb4 100644 --- a/Sources/libMultiMarkdown/html.c +++ b/Sources/libMultiMarkdown/html.c @@ -75,6 +75,19 @@ #define print_token(t) d_string_append_c_array(out, &(source[t->start]), t->len) #define print_localized(x) mmd_print_localized_char_html(out, x, scratch) + +/// strdup() not available on all platforms +static char * my_strdup(const char * source) { + char * result = malloc(strlen(source) + 1); + + if (result) { + strcpy(result, source); + } + + return result; +} + + // Use Knuth's pseudo random generator to obfuscate email addresses predictably long ran_num_next(); @@ -204,7 +217,7 @@ static char * strip_dimension_units(char *original) { char *result; int i; - result = strdup(original); + result = my_strdup(original); for (i = 0; result[i]; i++) result[i] = tolower(result[i]); @@ -1261,7 +1274,7 @@ void mmd_export_token_html(DString * out, const char * source, token * t, scratc if (strcmp(temp_char2, "notcited") == 0) { free(temp_char); - temp_char = strdup(""); + temp_char = my_strdup(""); temp_bool = false; } @@ -1272,7 +1285,7 @@ void mmd_export_token_html(DString * out, const char * source, token * t, scratc } else { // This is the actual citation (e.g. `[#foo]`) // No locator - temp_char = strdup(""); + temp_char = my_strdup(""); } // Classify this use diff --git a/Sources/libMultiMarkdown/latex.c b/Sources/libMultiMarkdown/latex.c index 0e350e5..e22e6dc 100644 --- a/Sources/libMultiMarkdown/latex.c +++ b/Sources/libMultiMarkdown/latex.c @@ -71,6 +71,18 @@ #define print_localized(x) mmd_print_localized_char_latex(out, x, scratch) +/// strdup() not available on all platforms +static char * my_strdup(const char * source) { + char * result = malloc(strlen(source) + 1); + + if (result) { + strcpy(result, source); + } + + return result; +} + + void mmd_print_char_latex(DString * out, char c) { switch (c) { case '\\': @@ -257,7 +269,7 @@ static char * correct_dimension_units(char *original) { char *result; int i; - result = strdup(original); + result = my_strdup(original); for (i = 0; result[i]; i++) result[i] = tolower(result[i]); @@ -1158,7 +1170,7 @@ void mmd_export_token_latex(DString * out, const char * source, token * t, scrat if (strcmp(temp_char2, "notcited") == 0) { free(temp_char); - temp_char = strdup(""); + temp_char = my_strdup(""); temp_bool = false; } @@ -1169,7 +1181,7 @@ void mmd_export_token_latex(DString * out, const char * source, token * t, scrat } else { // This is the actual citation (e.g. `[#foo]`) // No locator - temp_char = strdup(""); + temp_char = my_strdup(""); } // Classify this use diff --git a/Sources/libMultiMarkdown/mmd.c b/Sources/libMultiMarkdown/mmd.c index 0831a10..6c8e526 100644 --- a/Sources/libMultiMarkdown/mmd.c +++ b/Sources/libMultiMarkdown/mmd.c @@ -83,6 +83,17 @@ void ParseTrace(); void mmd_pair_tokens_in_block(token * block, token_pair_engine * e, stack * s); +/// strdup() not available on all platforms +static char * my_strdup(const char * source) { + char * result = malloc(strlen(source) + 1); + + if (result) { + strcpy(result, source); + } + + return result; +} + /// Build MMD Engine mmd_engine * mmd_engine_create(DString * d, unsigned long extensions) { @@ -2079,7 +2090,7 @@ char * mmd_string_metavalue_for_key(char * source, const char * key) { char * result; mmd_engine * e = mmd_engine_create_with_string(source, 0); - result = strdup(mmd_engine_metavalue_for_key(e, key)); + result = my_strdup(mmd_engine_metavalue_for_key(e, key)); mmd_engine_free(e, true); @@ -2315,6 +2326,6 @@ DString * mmd_engine_convert_to_data(mmd_engine * e, short format, const char * /// Return string containing engine version. char * mmd_version(void) { char * result; - result = strdup(MULTIMARKDOWN_VERSION); + result = my_strdup(MULTIMARKDOWN_VERSION); return result; } diff --git a/Sources/libMultiMarkdown/transclude.c b/Sources/libMultiMarkdown/transclude.c index e0dd839..644e68a 100644 --- a/Sources/libMultiMarkdown/transclude.c +++ b/Sources/libMultiMarkdown/transclude.c @@ -92,6 +92,18 @@ static char * my_strndup(const char * source, size_t n) { } +/// strdup() not available on all platforms +static char * my_strdup(const char * source) { + char * result = malloc(strlen(source) + 1); + + if (result) { + strcpy(result, source); + } + + return result; +} + + /// Windows can use either `\` or `/` as a separator -- thanks to t-beckmann on github /// for suggesting a fix for this. bool is_separator(char c) { @@ -211,7 +223,7 @@ void split_path_file(char ** dir, char ** file, const char * path) { slash++; *dir = my_strndup(path, slash - path); - *file = strdup(slash); + *file = my_strdup(slash); } #ifdef TEST @@ -421,7 +433,7 @@ void mmd_transclude_source(DString * source, const char * search_path, const cha // Add path to manifest if (add) - stack_push(manifest, strdup(file_path->str)); + stack_push(manifest, my_strdup(file_path->str)); } // Read the file diff --git a/Sources/libMultiMarkdown/writer.c b/Sources/libMultiMarkdown/writer.c index 95e1f42..c420f8d 100644 --- a/Sources/libMultiMarkdown/writer.c +++ b/Sources/libMultiMarkdown/writer.c @@ -87,6 +87,7 @@ void store_metadata(scratch_pad * scratch, meta * m); void store_abbreviation(scratch_pad * scratch, footnote * a); + /// strndup not available on all platforms static char * my_strndup(const char * source, size_t n) { size_t len = 0; @@ -113,6 +114,18 @@ static char * my_strndup(const char * source, size_t n) { } +/// strdup() not available on all platforms +static char * my_strdup(const char * source) { + char * result = malloc(strlen(source) + 1); + + if (result) { + strcpy(result, source); + } + + return result; +} + + /// Temporary storage while exporting parse tree to output format scratch_pad * scratch_pad_new(mmd_engine * e, short format) { scratch_pad * p = malloc(sizeof(scratch_pad)); @@ -534,7 +547,7 @@ attr * attr_new(char * key, char * value) { if (a) { a->key = key; - a->value = strdup(value); + a->value = my_strdup(value); a->next = NULL; } @@ -594,7 +607,7 @@ link * link_new(const char * source, token * label, char * url, char * title, ch l->label_text = NULL; } l->url = clean_string(url, false); - l->title = (title == NULL) ? NULL : strdup(title); + l->title = (title == NULL) ? NULL : my_strdup(title); l->attributes = (attributes == NULL) ? NULL : parse_attributes(attributes); } @@ -1577,7 +1590,7 @@ void process_metadata_stack(mmd_engine * e, scratch_pad * scratch) { free(temp_char); } else if (strcmp(m->key, "bibtex") == 0) { - scratch->bibtex_file = strdup(m->value); + scratch->bibtex_file = my_strdup(m->value); // Trigger complete document unless explicitly denied if (!(scratch->extensions & EXT_SNIPPET)) scratch->extensions |= EXT_COMPLETE; @@ -2386,7 +2399,7 @@ asset * asset_new(char * url, scratch_pad * scratch) { asset * a = malloc(sizeof(asset)); if (a) { - a->url = strdup(url); + a->url = my_strdup(url); // Create a unique local asset path a->asset_path = uuid_new();