From 71847ebdc6411ce9154e1448ea3120fd6a57b2a0 Mon Sep 17 00:00:00 2001 From: tobias Date: Sat, 13 Dec 2014 10:31:07 +0000 Subject: [PATCH] The function savestr allows NULL return values during Plan A patching so in case of out of memory conditions, Plan B can step in. In many cases, NULL value is not properly handled, so use xstrdup here (it's outside Plan A/B patching, which means that even Plan B relies on successful operations). --- usr.bin/patch/patch.c | 16 ++++++++-------- usr.bin/patch/pch.c | 14 +++++++------- usr.bin/patch/util.c | 18 +++++++++++++++++- usr.bin/patch/util.h | 3 ++- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/usr.bin/patch/patch.c b/usr.bin/patch/patch.c index ea7ba2ee275..d53bda30193 100644 --- a/usr.bin/patch/patch.c +++ b/usr.bin/patch/patch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: patch.c,v 1.53 2014/12/08 21:59:36 deraadt Exp $ */ +/* $OpenBSD: patch.c,v 1.54 2014/12/13 10:31:07 tobias Exp $ */ /* * patch - a program to apply diffs to original files @@ -213,7 +213,7 @@ main(int argc, char *argv[]) warn_on_invalid_line = true; if (outname == NULL) - outname = savestr(filearg[0]); + outname = xstrdup(filearg[0]); /* for ed script just up and do it and exit */ if (diff_type == ED_DIFF) { @@ -491,10 +491,10 @@ get_some_switches(void) /* FALLTHROUGH */ case 'z': /* must directly follow 'b' case for backwards compat */ - simple_backup_suffix = savestr(optarg); + simple_backup_suffix = xstrdup(optarg); break; case 'B': - origprae = savestr(optarg); + origprae = xstrdup(optarg); break; case 'c': diff_type = CONTEXT_DIFF; @@ -532,7 +532,7 @@ get_some_switches(void) case 'i': if (++filec == MAXFILEC) fatal("too many file arguments\n"); - filearg[filec] = savestr(optarg); + filearg[filec] = xstrdup(optarg); break; case 'l': canonicalize = true; @@ -544,7 +544,7 @@ get_some_switches(void) noreverse = true; break; case 'o': - outname = savestr(optarg); + outname = xstrdup(optarg); break; case 'p': strippath = atoi(optarg); @@ -588,12 +588,12 @@ get_some_switches(void) Argv += optind; if (Argc > 0) { - filearg[0] = savestr(*Argv++); + filearg[0] = xstrdup(*Argv++); Argc--; while (Argc > 0) { if (++filec == MAXFILEC) fatal("too many file arguments\n"); - filearg[filec] = savestr(*Argv++); + filearg[filec] = xstrdup(*Argv++); Argc--; } } diff --git a/usr.bin/patch/pch.c b/usr.bin/patch/pch.c index 8a1298c80b1..63f910ed5bc 100644 --- a/usr.bin/patch/pch.c +++ b/usr.bin/patch/pch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pch.c,v 1.48 2014/12/08 21:59:36 deraadt Exp $ */ +/* $OpenBSD: pch.c,v 1.49 2014/12/13 10:31:07 tobias Exp $ */ /* * patch - a program to apply diffs to original files @@ -215,14 +215,14 @@ there_is_another_patch(void) while (filearg[0] == NULL) { if (force || batch) { say("No file to patch. Skipping...\n"); - filearg[0] = savestr(bestguess); + filearg[0] = xstrdup(bestguess); skip_rest_of_patch = true; return true; } ask("File to patch: "); if (*buf != '\n') { free(bestguess); - bestguess = savestr(buf); + bestguess = xstrdup(buf); filearg[0] = fetchname(buf, &exists, 0); } if (!exists) { @@ -310,7 +310,7 @@ intuit_diff_type(void) else if (strnEQ(s, "Prereq:", 7)) { for (t = s + 7; isspace((unsigned char)*t); t++) ; - revision = savestr(t); + revision = xstrdup(t); for (t = revision; *t && !isspace((unsigned char)*t); t++) ; @@ -389,7 +389,7 @@ scan_exit: free(bestguess); bestguess = NULL; if (filearg[0] != NULL) - bestguess = savestr(filearg[0]); + bestguess = xstrdup(filearg[0]); else if (!ok_to_create_file) { /* * We don't want to create a new file but we need a @@ -1473,7 +1473,7 @@ posix_name(const struct file_name *names, bool assume_exists) path = names[NEW_FILE].path; } - return path ? savestr(path) : NULL; + return path ? xstrdup(path) : NULL; } static char * @@ -1538,7 +1538,7 @@ best_name(const struct file_name *names, bool assume_exists) names[NEW_FILE].path != NULL) best = names[NEW_FILE].path; } - return best ? savestr(best) : NULL; + return best ? xstrdup(best) : NULL; } static size_t diff --git a/usr.bin/patch/util.c b/usr.bin/patch/util.c index 876eda36e42..4db53a42b5b 100644 --- a/usr.bin/patch/util.c +++ b/usr.bin/patch/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.37 2014/11/22 15:49:28 tobias Exp $ */ +/* $OpenBSD: util.c,v 1.38 2014/12/13 10:31:07 tobias Exp $ */ /* * patch - a program to apply diffs to original files @@ -191,6 +191,22 @@ savestr(const char *s) return rv; } +/* + * Allocate a unique area for a string. Call fatal if out of memory. + */ +char * +xstrdup(const char *s) +{ + char *rv; + + if (!s) + s = "Oops"; + rv = strdup(s); + if (rv == NULL) + fatal("out of memory\n"); + return rv; +} + /* * Vanilla terminal output (buffered). */ diff --git a/usr.bin/patch/util.h b/usr.bin/patch/util.h index b27bbe3cc3a..5cd0ba84057 100644 --- a/usr.bin/patch/util.h +++ b/usr.bin/patch/util.h @@ -1,4 +1,4 @@ -/* $OpenBSD: util.h,v 1.15 2005/06/20 07:14:06 otto Exp $ */ +/* $OpenBSD: util.h,v 1.16 2014/12/13 10:31:07 tobias Exp $ */ /* * patch - a program to apply diffs to original files @@ -40,6 +40,7 @@ void pfatal(const char *, ...) void ask(const char *, ...) __attribute__((__format__(__printf__, 1, 2))); char *savestr(const char *); +char *xstrdup(const char *); void set_signals(int); void ignore_signals(void); void makedirs(const char *, bool); -- 2.20.1