shithub: fnt

Download patch

ref: 1f8bdfba6a4301d5197fc4ff62561a31cc958e50
parent: 195e8566f95f1b519335cc875cc88c8eea2fa5a6
author: Sigrid Solveig Haflínudóttir <sigrid@ftrv.se>
date: Fri Jul 12 21:15:02 EDT 2024

fix a few possible gotchas detected by clang analyzer

--- a/otf.c.in
+++ b/otf.c.in
@@ -571,7 +571,7 @@
 		return g;
 
 	if(otfpushrange(o, o->glyf.offset, o->glyf.length) < 0)
-		return nil;
+		goto err;
 	if(otfpushrange(o, off, len) < 0)
 		goto err;
 	if(read_Glyf(o, g) < 0){
--- a/plan9/test.c
+++ b/plan9/test.c
@@ -118,7 +118,7 @@
 			for(i = 0; i < n; i++){
 				Glyf *g = otfglyf(o, i);
 				if(g == nil)
-					continue;
+					sysfatal("%r");
 				if(h > 0 && g->simple != nil && g->numberOfContours > 0){
 					int w;
 					u8int *b = otfdrawglyf(g, h, &w);
--- a/rast.c
+++ b/rast.c
@@ -452,12 +452,13 @@
 u8int *
 otfdrawglyf(Glyf *g, int h, int *wo)
 {
-	SegQ *s₀, *s;
-	Sval *fp;
+	int w, i, r, k, npts, npx, ns;
 	Point *p₀, *p, *prev;
 	Spt *pts₀, *pts, *e;
-	int w, i, r, k, npts, npx, ns;
 	double scale;
+	SegQ *s₀, *s;
+	Sval *fp;
+	u8int *b;
 
 	/* FIXME those epsilons and ceil is a rather dumb hack for now */
 	scale = (double)h / (g->yMax - g->yMin) + ε;
@@ -464,7 +465,6 @@
 	w = *wo = ceil((g->xMax - g->xMin) * scale + ε);
 
 	npx = w * h;
-	fp = calloc(1, npx*sizeof(*fp));
 	ns = 0;
 	s₀ = nil;
 
@@ -472,6 +472,8 @@
 		npts = g->simple->endPtsOfContours[k]+1;
 		if(k > 0)
 			npts -= g->simple->endPtsOfContours[k-1]+1;
+		if(npts < 2)
+			continue;
 
 		pts₀ = e = malloc((npts+2)*3*sizeof(*e));
 		p₀ = p = g->simple->points + (k > 0 ? g->simple->endPtsOfContours[k-1]+1 : 0);
@@ -495,7 +497,7 @@
 			}
 			*e = (Spt){p->x, p->y};
 		}
-		if(e[-1].x != pts₀->x || e[-1].y != pts₀->y){
+		if(e > pts₀ && (e[-1].x != pts₀->x || e[-1].y != pts₀->y)){
 			if(p[-1].onCurve) /* close with a straight line */
 				*e++ = *pts₀;
 			*e++ = *pts₀;
@@ -515,16 +517,17 @@
 		free(pts₀);
 	}
 
+	fp = calloc(1, npx*sizeof(*fp));
 	r = qbzr(s₀, ns, w, h, fp);
 	free(s₀);
 
-	if(r != 0)
-		return nil;
-
-	u8int *b = malloc(npx);
-	for(i = 0; i < npx; i++){
-		b[i] = fp[i] <= 0 ? 0 : (fp[i] >= QBZR_PIX_SCALE ? 255 : fp[i]/QBZR_PIX_SCALE*255);
-		b[i] = 255 - b[i];
+	b = nil;
+	if(r == 0){
+		b = (u8int*)fp;
+		for(i = 0; i < npx; i++)
+			b[i] = 255 - (fp[i] <= 0 ? 0 : (fp[i] >= QBZR_PIX_SCALE ? 255 : fp[i]/QBZR_PIX_SCALE*255));
+		if((b = realloc(b, npx)) != nil)
+			fp = nil;
 	}
 	free(fp);
 	return b;
--- a/unix/test.c
+++ b/unix/test.c
@@ -144,13 +144,15 @@
 			Image *im = h > 0 ? calloc(n, sizeof(*im)) : NULL;
 			for(i = 0; i < n; i++){
 				Glyf *g = otfglyf(o, i);
-				if(g == NULL)
-					continue;
+				if(g == NULL){
+					fprintf(stderr, "glyph %d: failed to parse\n", i);
+					exit(1);
+				}
 				if(h > 0 && g->simple != NULL && g->numberOfContours > 0){
 					int w;
 					u8int *b = otfdrawglyf(g, h, &w);
 					if(b == NULL){
-						fprintf(stderr, "failed\n");
+						fprintf(stderr, "glyph %d: failed to draw\n", i);
 						exit(1);
 					}
 					im[i].w = w;