git: 9front

Download patch

ref: 3d3439a70c9033920587f9871c6a20379850dda4
parent: 512ed418b64e0aed4ef0bbf3730717a06fcd1bec
author: qwx <qwx@sciops.net>
date: Mon Oct 20 19:10:16 EDT 2025

vdiff: fix memory leaks and check alloc functions return values

a950d02475406191882f98499eed95faff0f0104 swapped a freeimage()
with a free() by mistake.  this also fixes some unrelated
Brdstr leaks.

--- a/sys/src/cmd/vdiff.c
+++ b/sys/src/cmd/vdiff.c
@@ -120,6 +120,17 @@
 	return q;
 }
 
+Image*
+eallocimage(Rectangle r, int repl, ulong n)
+{
+	Image *i;
+
+	i = allocimage(display, r, screen->chan, repl, n);
+	if(i == nil)
+		sysfatal("allocimage: %r");
+	return i;
+}
+
 void
 plumb(char *f, int l)
 {
@@ -337,10 +348,8 @@
 		clampoffset(1);
 	else
 		clampoffset(0);
-	free(fb);
-	fb = allocimage(display, screen->r, screen->chan, 0, DBlack);
-	if(fb == nil)
-		sysfatal("allocimage: %r");
+	freeimage(fb);
+	fb = eallocimage(screen->r, 0, DBlack);
 	redraw();
 }
 
@@ -392,7 +401,8 @@
 		if(i >= npatches || npatches == 1)
 			return nil;
 		if(patches[i].name == nil)
-			patches[i].name = smprint("%d", i);
+			if((patches[i].name = smprint("%d", i)) == nil)
+				sysfatal("smprint: %r");
 		return patches[i].name;
 	}
 }
@@ -489,29 +499,24 @@
 	oldbuttons = m.buttons;
 }
 
-Image*
-ecolor(ulong n)
-{
-	Image *i;
-
-	i = allocimage(display, Rect(0,0,1,1), screen->chan, 1, n);
-	if(i == nil)
-		sysfatal("allocimage: %r");
-	return i;
-}
-
 void
 initcol(Col *c, ulong fg, ulong bg)
 {
-	c->fg = ecolor(fg);
-	c->bg = ecolor(bg);
+	Rectangle r;
+
+	r = Rect(0, 0, 1, 1);
+	c->fg = eallocimage(r, 1, fg);
+	c->bg = eallocimage(r, 1, bg);
 }
 
 void
 initcols(int black)
 {
+	Rectangle r;
+
+	r = Rect(0, 0, 1, 1);
 	if(black){
-		bord = ecolor(0x888888FF^(~0xFF));
+		bord = eallocimage(r, 1, 0x888888FF^(~0xFF));
 		initcol(&scrlcol,     DBlack, 0x999999FF^(~0xFF));
 		initcol(&cols[Lfile], DWhite, 0x333333FF);
 		initcol(&cols[Lsep],  DBlack, DPurpleblue);
@@ -518,9 +523,9 @@
 		initcol(&cols[Ladd],  DWhite, 0x002800FF);
 		initcol(&cols[Ldel],  DWhite, 0x3F0000FF);
 		initcol(&cols[Lnone], DWhite, DBlack);
-		trlcol = ecolor(0x9F0000FF);
+		trlcol = eallocimage(r, 1, 0x9F0000FF);
 	}else{
-		bord = ecolor(0x888888FF);
+		bord = eallocimage(r, 1, 0x888888FF);
 		initcol(&scrlcol,     DWhite, 0x999999FF);
 		initcol(&cols[Lfile], DBlack, 0xEFEFEFFF);
 		initcol(&cols[Lsep],  DBlack, 0xEAFFFFFF);
@@ -527,7 +532,7 @@
 		initcol(&cols[Ladd],  DBlack, 0xE6FFEDFF);
 		initcol(&cols[Ldel],  DBlack, 0xFFEEF0FF);
 		initcol(&cols[Lnone], DBlack, DWhite);
-		trlcol = ecolor(0xFF8890FF);
+		trlcol = eallocimage(r, 1, 0xFF8890FF);
 	}
 }
 
@@ -539,9 +544,7 @@
 
 	w = font->height;
 	h = font->height;
-	expander[0] = allocimage(display, Rect(0, 0, w, h), screen->chan, 0, DNofill);
-	if(expander[0] == nil)
-		sysfatal("allocimage: %r");
+	expander[0] = eallocimage(Rect(0, 0, w, h), 0, DNofill);
 	draw(expander[0], expander[0]->r, cols[Lfile].bg, nil, ZP);
 	p[0] = Pt(0.25*w, 0.25*h);
 	p[1] = Pt(0.25*w, 0.75*h);
@@ -548,9 +551,7 @@
 	p[2] = Pt(0.75*w, 0.5*h);
 	p[3] = p[0];
 	fillpoly(expander[0], p, 4, 0, bord, ZP);
-	expander[1] = allocimage(display, Rect(0, 0, w, h), screen->chan, 0, DNofill);
-	if(expander[1] == nil)
-		sysfatal("allocimage: %r");
+	expander[1] = eallocimage(Rect(0, 0, w, h), 0, DNofill);
 	draw(expander[1], expander[1]->r, cols[Lfile].bg, nil, ZP);
 	p[0] = Pt(0.25*w, 0.25*h);
 	p[1] = Pt(0.75*w, 0.25*h);
@@ -621,6 +622,8 @@
 	int n, l;
 
 	p = strdup(s);
+	if(p == nil)
+		sysfatal("strdup: %r");
 	n = tokenize(p, t, 5);
 	if(n<=0)
 		return -1;
@@ -653,10 +656,11 @@
 			gotterm = 1;
 			/* remove '--' and extra newline */
 			b->nlines--;
+			free(s);
 			free(Brdstr(bp, '\n', 1));
 		New:
 			npatches++;
-			patches = realloc(patches, sizeof *patches * npatches);
+			patches = erealloc(patches, sizeof *patches * npatches);
 			cur = patches+npatches-1;
 			cur->blocks = nil;
 			cur->nblocks = 0;
@@ -663,7 +667,7 @@
 			cur->name = name;
 			b = addblock();
 			n = 0;
-			ab = 0;		
+			ab = 0;
 			break;
 		case Lfile:
 			if(s[0] == '-'){
@@ -685,7 +689,10 @@
 					*tab = 0;
 				if(strcmp(f, "/dev/null") != 0)
 					b->f = f;
-			}
+				else
+					free(s);
+			}else
+				free(s);
 			break;
 		case Lsep:
 			n = lineno(s) - 1; /* -1 as the separator is not an actual line */
@@ -699,6 +706,8 @@
 						cur->name = smprint("%s %.*s", name, 9, f);
 					else
 						cur->name = smprint("%.*s", 9, f);
+					if(cur->name == nil)
+						sysfatal("smprint: %r");
 				}
 			}
 			t = Lnone;
--