Index | Thread | Search

From:
"Omar Polo" <op@omarpolo.com>
Subject:
Re: patch: bail out in fetchname() if out_of_memory
To:
Omar Polo <op@omarpolo.com>
Cc:
tech@openbsd.org
Date:
Thu, 05 Nov 2026 11:59:50 +0100

Download raw body.

Thread
"Omar Polo" <op@omarpolo.com> wrote:
> "Omar Polo" <op@omarpolo.com> wrote:
> > savestr() is basically xstrdup() with an optional path for "plan A"
> > where it returns NULL instead of fatal().
> > 
> > afaik, this is the only place where we don't check out_of_mem after
> > savestr().  bailing out seems nicer than crashing on the next line.
> > 
> > ok?
> 
> forgot the other hunk in fetchname(), sorry.

and now without the trivial leak; apologize:

-----------------------------------------------
commit d57a8204d22073579de23164f6833b155b5ffe91 (master)
from: Omar Polo <op@omarpolo.com>
date: Thu Nov  5 10:57:27 2026 UTC
 
 bail out on fetchname on OOM
 
diff df4e48f5c58474cd91cf83065e253c9efdfe5d93 d57a8204d22073579de23164f6833b155b5ffe91
commit - df4e48f5c58474cd91cf83065e253c9efdfe5d93
commit + d57a8204d22073579de23164f6833b155b5ffe91
blob - c94a3f2beb10a18d9687941619e72776bf160a7f
blob + 917bd11087a4650dcff2b36a1dedaba9d3122e42
--- usr.bin/patch/util.c
+++ usr.bin/patch/util.c
@@ -356,6 +356,8 @@ fetchname(const char *at, bool *exists, int strip_lead
 	if (strnEQ(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
 		return NULL;
 	name = fullname = t = savestr(at);
+	if (out_of_mem)
+		return NULL;
 
 	tab = strchr(t, '\t') != NULL;
 	/* Strip off up to `strip_leading' path components and NUL terminate. */
@@ -381,6 +383,8 @@ fetchname(const char *at, bool *exists, int strip_lead
 	}
 	name = savestr(name);
 	free(fullname);
+	if (out_of_mem)
+		return NULL;
 
 	*exists = stat(name, &filestat) == 0;
 	return name;