From owner-svn-src-all@FreeBSD.ORG Thu Dec 25 21:51:30 2014 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id E278463F; Thu, 25 Dec 2014 21:51:30 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id CE7981A12; Thu, 25 Dec 2014 21:51:30 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.9/8.14.9) with ESMTP id sBPLpUsi068555; Thu, 25 Dec 2014 21:51:30 GMT (envelope-from pfg@FreeBSD.org) Received: (from pfg@localhost) by svn.freebsd.org (8.14.9/8.14.9/Submit) id sBPLpTbM068550; Thu, 25 Dec 2014 21:51:29 GMT (envelope-from pfg@FreeBSD.org) Message-Id: <201412252151.sBPLpTbM068550@svn.freebsd.org> X-Authentication-Warning: svn.freebsd.org: pfg set sender to pfg@FreeBSD.org using -f From: "Pedro F. Giffuni" Date: Thu, 25 Dec 2014 21:51:29 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r276218 - head/usr.bin/patch X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 25 Dec 2014 21:51:31 -0000 Author: pfg Date: Thu Dec 25 21:51:28 2014 New Revision: 276218 URL: https://svnweb.freebsd.org/changeset/base/276218 Log: patch: Bring in xstrdup and use it when appropriate. 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). Clean up some whitespaces while here Obtained from: OpenBSD MFC after: 2 weeks Modified: head/usr.bin/patch/patch.c head/usr.bin/patch/pch.c head/usr.bin/patch/util.c head/usr.bin/patch/util.h Modified: head/usr.bin/patch/patch.c ============================================================================== --- head/usr.bin/patch/patch.c Thu Dec 25 21:34:29 2014 (r276217) +++ head/usr.bin/patch/patch.c Thu Dec 25 21:51:28 2014 (r276218) @@ -23,7 +23,7 @@ * -C option added in 1998, original code by Marc Espie, based on FreeBSD * behaviour * - * $OpenBSD: patch.c,v 1.52 2014/11/26 18:34:51 millert Exp $ + * $OpenBSD: patch.c,v 1.54 2014/12/13 10:31:07 tobias Exp $ * $FreeBSD$ * */ @@ -215,13 +215,13 @@ main(int argc, char *argv[]) for (open_patch_file(filearg[1]); there_is_another_patch(); reinitialize_almost_everything()) { /* for each patch in patch file */ - + patch_seen = true; 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) { @@ -416,7 +416,7 @@ main(int argc, char *argv[]) } set_signals(1); } - + if (!patch_seen) error = 2; @@ -514,10 +514,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; @@ -555,7 +555,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; @@ -567,7 +567,7 @@ get_some_switches(void) noreverse = true; break; case 'o': - outname = savestr(optarg); + outname = xstrdup(optarg); break; case 'p': strippath = atoi(optarg); @@ -611,12 +611,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--; } } Modified: head/usr.bin/patch/pch.c ============================================================================== --- head/usr.bin/patch/pch.c Thu Dec 25 21:34:29 2014 (r276217) +++ head/usr.bin/patch/pch.c Thu Dec 25 21:51:28 2014 (r276218) @@ -205,14 +205,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) { @@ -319,7 +319,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++) ; @@ -403,7 +403,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 @@ -1505,7 +1505,7 @@ posix_name(const struct file_name *names path = names[NEW_FILE].path; } - return path ? savestr(path) : NULL; + return path ? xstrdup(path) : NULL; } static char * @@ -1571,7 +1571,7 @@ best_name(const struct file_name *names, best = names[NEW_FILE].path; } - return best ? savestr(best) : NULL; + return best ? xstrdup(best) : NULL; } static size_t @@ -1613,7 +1613,7 @@ strtolinenum(char *nptr, char **endptr) if (errstr != NULL) fatal("invalid line number at line %ld: `%s' is %s\n", p_input_line, nptr, errstr); - + *p = c; *endptr = p; Modified: head/usr.bin/patch/util.c ============================================================================== --- head/usr.bin/patch/util.c Thu Dec 25 21:34:29 2014 (r276217) +++ head/usr.bin/patch/util.c Thu Dec 25 21:51:28 2014 (r276218) @@ -202,6 +202,22 @@ savestr(const char *s) } /* + * 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). */ void Modified: head/usr.bin/patch/util.h ============================================================================== --- head/usr.bin/patch/util.h Thu Dec 25 21:34:29 2014 (r276217) +++ head/usr.bin/patch/util.h Thu Dec 25 21:51:28 2014 (r276218) @@ -23,7 +23,7 @@ * -C option added in 1998, original code by Marc Espie, based on FreeBSD * behaviour * - * $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 $ * $FreeBSD$ */ @@ -41,6 +41,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);