shithub: lola

Download patch

ref: b4fd6f903bfb756d83438146eadeabe6badc32a4
parent: 7b72e7637b8b59b1c30603d310be1b293f620f34
author: aap <aap@papnet.eu>
date: Fri Mar 15 12:28:45 EDT 2024

fixed use after free bug. some cleanup.

--- a/inc.h
+++ b/inc.h
@@ -16,7 +16,8 @@
 	FALSE = 0,
 	TRUE = 1,
 
-	BIG = 3
+	BIG = 3,
+	MAXWINDOWS = 1000
 };
 
 #define ALT(c, v, t) (Alt){ c, v, t, nil, nil, 0 }
@@ -244,10 +245,8 @@
 };
 
 extern Window *bottomwin, *topwin;
-extern Window *windows[1000];	// TMP
+extern Window *windows[MAXWINDOWS];
 extern int nwindows;
-extern Window *hidden[1000];
-extern int nhidden;
 extern Window *focused, *cursorwin;
 
 void wdecor(Window *w);
--- a/main.c
+++ b/main.c
@@ -450,7 +450,7 @@
 	Exit
 };
 int Hidden = Exit+1;
-char *menu3str[7 + nelem(hidden)] = {
+char *menu3str[7 + MAXWINDOWS] = {
 	"New",
 	"Resize",
 	"Move",
@@ -508,6 +508,8 @@
 void
 btn3menu(void)
 {
+	static Window *hidden[MAXWINDOWS];
+	int nhidden;
 	Window *w, *t;
 	int i, sel;
 
@@ -636,14 +638,8 @@
 		draw(screen, screen->r, background, nil, ZP);
 
 		delta = subpt(nr.min, or.min);
-		for(w = bottomwin; w; w = w->higher){
-			Rectangle r = w->img->r;
-			freeimage(w->img);
-			w->img = nil;
-			wresize(w, rectaddpt(r, delta));
-			if(w->hidden)
-				originwindow(w->img, w->img->r.min, screen->r.max);
-		}
+		for(w = bottomwin; w; w = w->higher)
+			wresize(w, rectaddpt(w->img->r, delta));
 
 		flushimage(display, 1);
 	}
--- a/wind.c
+++ b/wind.c
@@ -1,10 +1,8 @@
 #include "inc.h"
 
 Window *bottomwin, *topwin;
-Window *windows[1000];	// TMP
+Window *windows[MAXWINDOWS];
 int nwindows;
-Window *hidden[1000];
-int nhidden;
 Window *focused, *cursorwin;
 
 static void winthread(void *arg);
@@ -114,7 +112,8 @@
 	wsetsize(w, r);
 	wdecor(w);
 	wlistpushfront(w);
-	// TMP - make dynamic
+	// TODO: could be more graceful here
+	assert(nwindows < MAXWINDOWS);
 	windows[nwindows++] = w;
 
 	w->mc.c = chancreate(sizeof(Mouse), 16);
@@ -197,8 +196,12 @@
 wrelease(Window *w)
 {
 	if(decref(w) == 0){
+		/* increment ref count temporarily
+		 * so win thread doesn't exit too early */
+		incref(w);
 		wunfocus(w);
 		wclose(w);
+		decref(w);
 		if(!inwinthread(w))
 			wsendmsg(w, Wakeup);
 	}else