From 05ae0d42a944f6c7d940a5e58eb90b619dadbfdb Mon Sep 17 00:00:00 2001 From: rodri Date: Mon, 5 Aug 2024 10:41:11 +0000 Subject: fix a double-free. add a generic value-swapping macro. --- clip.c | 20 ++++---------------- internal.h | 6 +++--- render.c | 12 ++++++------ util.c | 20 -------------------- vertex.c | 10 ---------- 5 files changed, 13 insertions(+), 55 deletions(-) diff --git a/clip.c b/clip.c index 94f84a6..aaa8754 100644 --- a/clip.c +++ b/clip.c @@ -33,16 +33,6 @@ addvert(Polygon *p, Vertex v) return p->n; } -static void -swappoly(Polygon **a, Polygon **b) -{ - Polygon *tmp; - - tmp = *a; - *a = *b; - *b = tmp; -} - static void cleanpoly(Polygon *p) { @@ -128,7 +118,7 @@ allin: } cleanpoly(Vin); if(j < 6-1) - swappoly(&Vin, &Vout); + SWAP(Polygon*, &Vin, &Vout); } if(Vout->n < 2) @@ -213,8 +203,6 @@ adjustverts(Point *p0, Point *p1, Vertex *v0, Vertex *v1) 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 @@ rectclipline(Rectangle r, Point *p0, Point *p1, Vertex *v0, Vertex *v1) 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){ diff --git a/internal.h b/internal.h index 6cd512c..8b6784f 100644 --- a/internal.h +++ b/internal.h @@ -45,7 +45,6 @@ void rmfbctl(Framebufctl*); /* 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 @@ int rectclipline(Rectangle, Point*, Point*, Vertex*, Vertex*); /* 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 @@ uvlong nanosec(void); #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;} diff --git a/render.c b/render.c index a28ed1b..41b0939 100644 --- a/render.c +++ b/render.c @@ -135,14 +135,14 @@ rasterize(Rastertask *task) /* 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 @@ rasterize(Rastertask *task) 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 @@ rasterize(Rastertask *task) 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){ diff --git a/util.c b/util.c index 2ddf21c..b536147 100644 --- a/util.c +++ b/util.c @@ -32,26 +32,6 @@ fmax(double a, double b) 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) { diff --git a/vertex.c b/vertex.c index 92c6b0a..970bbbc 100644 --- a/vertex.c +++ b/vertex.c @@ -43,16 +43,6 @@ dupvertex(Vertex *v) return nv; } -void -swapvertex(Vertex *a, Vertex *b) -{ - Vertex t; - - t = *a; - *a = *b; - *b = t; -} - /* * linear attribute interpolation */ -- cgit v1.2.3