plan9fox/sys/src
Igor Böhm efa6937460 acme: fix leaking memory allocated by getenv("font")
If the font chosen for acme is retrieved via `getenv("font")` its
memory is leaked:

<snip>
	if(fontnames[0] == nil)
		fontnames[0] = getenv("font");
		^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> getenv(…) mallocs memory

	if(fontnames[0] == nil)
		fontnames[0] = "/lib/font/bit/vga/unicode.font";
	if(access(fontnames[0], 0) < 0){
		fprint(2, "acme: can't access %s: %r\n", fontnames[0]);
		exits("font open");
	}
	if(fontnames[1] == nil)
		fontnames[1] = fontnames[0];
	fontnames[0] = estrdup(fontnames[0]);
	^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> if the `getenv("font")` path was taken above, this assignment
> will leak its memory.
</snap>

The following leak/acid session demonstrates the issue:

<snip>
cpu% leak -s 212252
src(0x002000cb); // 1
cpu% acid 212252
/proc/212252/text:amd64 plan 9 executable
/sys/lib/acid/port
/sys/lib/acid/amd64
acid: src(0x002000cb)
/sys/src/cmd/acme/acme.c:107
 102			fprint(2, "usage: acme [-aib] [-c ncol] [-f font] [-F fixedfont] [-l loadfile | file...]\n");
 103			exits("usage");
 104		}ARGEND
 105
 106		if(fontnames[0] == nil)
>107			fontnames[0] = getenv("font");
 108		if(fontnames[0] == nil)
 109			fontnames[0] = "/lib/font/bit/vga/unicode.font";
 110		if(access(fontnames[0], 0) < 0){
 111			fprint(2, "acme: can't access %s: %r\n", fontnames[0]);
 112			exits("font open");
acid:
</snap>

The fix tries to first check if a font has been set via
command line options in which case the font string is
malloced via estrdup(…).

If no font has been selected on the command line getenv("font")
is used. If no getenv("font") var is found we malloc a default
font via estrdup(…).

<snip>
	if(fontnames[0] != nil)
		fontnames[0] = estrdup(fontnames[0]);
	else
		if((fontnames[0] = getenv("font")) == nil)
			fontnames[0] = estrdup("/lib/font/bit/vga/unicode.font");
	if(access(fontnames[0], 0) < 0){
		fprint(2, "acme: can't access %s: %r\n", fontnames[0]);
		exits("font open");
	}
	if(fontnames[1] == nil)
		fontnames[1] = fontnames[0];
	fontnames[1] = estrdup(fontnames[1]);
</snap>

This resolves the memory leak reported by leak(1).
2021-11-05 23:51:55 +00:00
..
9 audiohda: add support for intel comet lake-s, fix typo 2021-11-05 22:26:28 +00:00
ape ape: add missing idn2utf, utf2idn to lib9 2021-10-31 04:42:34 +00:00
boot 9boot: fix isowalk() for directories spanning multiple sectors 2021-09-11 12:26:35 +00:00
cmd acme: fix leaking memory allocated by getenv("font") 2021-11-05 23:51:55 +00:00
games add joy(1): joystick script for all emulators, and docs 2021-09-01 22:47:30 +00:00
lib9p lib9p: remove Srv.srvfd, make postsrv() and threadpostsrv() return the mountable file descriptor, update documentation 2021-05-01 19:58:58 +02:00
libaml libaml: resolve refs in ObjectType instruction 2021-10-17 19:52:28 +00:00
libauth libauth: re-implement procsetuser() to use /proc instead of #c/user 2020-12-23 13:10:30 +01:00
libauthsrv libauthsrv: open internal file-descriptors with OCEXEC flag 2020-12-07 16:47:06 +01:00
libavl
libbin
libbio
libc libc: idn2utf()/utf2idn(): check for < 1 buffer, can't insert terminating NUL. 2021-10-31 12:43:47 +00:00
libcomplete libcomplete: open internal file-descriptor with OCEXEC flag 2020-12-07 17:18:33 +01:00
libcontrol
libdisk libdisk: catch null bytes in proto file, fix memory leaks 2018-10-20 20:32:22 +02:00
libdraw libdraw: enter/eenter: fix ^W removing the text on the right side of the tick 2021-02-04 10:57:37 +01:00
libdtracy dtracy: avoid dmachlock() race 2019-03-30 09:17:46 +01:00
libfis
libflate
libframe
libgeometry qball: remove unused #include <stdio.h> 2020-08-04 16:27:27 +02:00
libhtml libhtml: increase Nestmax to render some convoluted pages (thanks fernan) 2018-09-23 23:35:41 +02:00
libhttpd always zero initialize Tm structure for tm2sec() 2019-12-03 08:22:02 +01:00
libip libip: move optimized 386 assembly version of ptclbsum() from kernel to libip 2019-02-27 18:29:08 +01:00
libjson
liblex
libmach libmach: remove newline 2019-05-09 10:27:17 +02:00
libmemdraw libmemdraw: remove inexistent build targets 2020-04-19 15:30:58 +02:00
libmemlayer
libmp libmp: add dummy mkfile for arm64 2019-05-03 21:02:13 +02:00
libndb libndb: move mkptrname() into libndb to avoid duplication 2021-11-03 19:38:36 +00:00
libplumb
libregexp
libsat
libscribble
libsec libsec: no point in checking for "xn--" marker, just call idn2utf(). 2021-10-31 12:40:47 +00:00
libstdio libstdio: sync bits of vfprintf from APE 2021-02-19 23:04:09 +01:00
libString
libsunrpc libsunrpc: work around arm64 compiler bug in sunStringUnpack() 2021-03-29 17:13:50 +02:00
libthread libthread: fix debug prints, simplify 2021-10-13 17:08:26 +00:00
libttf libttf: fix cmap subtable offset type and rank UCS-4 higher (more codepoints). fixes runes > 0xffff 2020-08-05 14:25:21 +02:00
libventi venti: fix memory layers 2021-07-21 05:06:05 +00:00
mkfile revert 8c228a123f: accidental commit 2021-08-22 17:47:38 +00:00
mkfile.proto /sys/src: add arm64 to installall list 2021-06-21 03:34:47 +00:00