shithub: libgraphics

Download patch

ref: 05ae0d42a944f6c7d940a5e58eb90b619dadbfdb
parent: c06379ee3aa51d3adf456c8d126feebd1da9de70
author: rodri <rgl@antares-labs.eu>
date: Mon Aug 5 06:41:11 EDT 2024

fix a double-free. add a generic value-swapping macro.

--- a/clip.c
+++ b/clip.c
@@ -34,16 +34,6 @@
 }
 
 static void
-swappoly(Polygon **a, Polygon **b)
-{
-	Polygon *tmp;
-
-	tmp = *a;
-	*a = *b;
-	*b = tmp;
-}
-
-static void
 cleanpoly(Polygon *p)
 {
 	int i;
@@ -128,7 +118,7 @@
 		}
 		cleanpoly(Vin);
 		if(j < 6-1)
-			swappoly(&Vin, &Vout);
+			SWAP(Polygon*, &Vin, &Vout);
 	}
 
 	if(Vout->n < 2)
@@ -213,8 +203,6 @@
 	perc = len == 0? 0: hypot(Δp.x, Δp.y)/len;
 	lerpvertex(&v[1], v0, v1, perc);
 
-	delvattrs(v0);
-	delvattrs(v1);
 	*v0 = dupvertex(&v[0]);
 	*v1 = dupvertex(&v[1]);
 }
@@ -244,9 +232,9 @@
 			return -1;
 
 		if(ptisinside(code0)){
-			swappt(p0, p1);
-			swapi(&code0, &code1);
-			swapvertex(v0, v1);
+			SWAP(Point, p0, p1);
+			SWAP(int, &code0, &code1);
+			SWAP(Vertex, v0, v1);
 		}
 
 		if(code0 & CLIPL){
--- a/internal.h
+++ b/internal.h
@@ -45,7 +45,6 @@
 
 /* vertex */
 Vertex dupvertex(Vertex*);
-void swapvertex(Vertex*, Vertex*);
 void lerpvertex(Vertex*, Vertex*, Vertex*, double);
 void berpvertex(Vertex*, Vertex*, Vertex*, Vertex*, Point3);
 void delvattrs(Vertex*);
@@ -58,8 +57,6 @@
 /* util */
 int min(int, int);
 int max(int, int);
-void swapi(int*, int*);
-void swappt(Point*, Point*);
 void memsetf(void*, float, usize);
 void memsetl(void*, ulong, usize);
 
@@ -70,3 +67,6 @@
 #define getpixel(fb, p) (((fb)->cb)[Dx((fb)->r)*(p).y + (p).x])
 /* void putpixel(Framebuf *fb, Point p, ulong c) */
 #define putpixel(fb, p, c) (((fb)->cb)[Dx((fb)->r)*(p).y + (p).x] = (c))
+
+/* void SWAP(type t, type *a, type *b) */
+#define SWAP(t, a, b) {t tmp; tmp = *(a); *(a) = *(b); *(b) = tmp;}
--- a/render.c
+++ b/render.c
@@ -135,14 +135,14 @@
 		/* transpose the points */
 		if(abs(p0.x-p1.x) < abs(p0.y-p1.y)){
 			steep = 1;
-			swapi(&p0.x, &p0.y);
-			swapi(&p1.x, &p1.y);
+			SWAP(int, &p0.x, &p0.y);
+			SWAP(int, &p1.x, &p1.y);
 		}
 
 		/* make them left-to-right */
 		if(p0.x > p1.x){
-			swappt(&p0, &p1);
-			swapvertex(&prim.v[0], &prim.v[1]);
+			SWAP(Point, &p0, &p1);
+			SWAP(Vertex, &prim.v[0], &prim.v[1]);
 		}
 
 		dp = subpt(p1, p0);
@@ -155,7 +155,7 @@
 			dplen = hypot(dp.x, dp.y);
 			perc = dplen == 0? 0: hypot(Δp.x, Δp.y)/dplen;
 
-			if(steep) swapi(&p.x, &p.y);
+			if(steep) SWAP(int, &p.x, &p.y);
 
 			z = flerp(prim.v[0].p.z, prim.v[1].p.z, perc);
 			/* TODO get rid of the bounds check and make sure the clipping doesn't overflow */
@@ -176,7 +176,7 @@
 			pixel(params->fb, p, c);
 			delvattrs(&fsp.v);
 discard:
-			if(steep) swapi(&p.x, &p.y);
+			if(steep) SWAP(int, &p.x, &p.y);
 
 			e += Δe;
 			if(e > dp.x){
--- a/util.c
+++ b/util.c
@@ -32,26 +32,6 @@
 	return a > b? a: b;
 }
 
-void
-swapi(int *a, int *b)
-{
-	int t;
-
-	t = *a;
-	*a = *b;
-	*b = t;
-}
-
-void
-swappt(Point *a, Point *b)
-{
-	Point t;
-
-	t = *a;
-	*a = *b;
-	*b = t;
-}
-
 Point2
 modulapt2(Point2 a, Point2 b)
 {
--- a/vertex.c
+++ b/vertex.c
@@ -43,16 +43,6 @@
 	return nv;
 }
 
-void
-swapvertex(Vertex *a, Vertex *b)
-{
-	Vertex t;
-
-	t = *a;
-	*a = *b;
-	*b = t;
-}
-
 /*
  * linear attribute interpolation
  */