source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mdocml: Auditing the tbl(7) code for more NULL pointer accesses, i came
@ 2015-01-30  2:09 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2015-01-30  2:09 UTC (permalink / raw)
  To: source

Log Message:
-----------
Auditing the tbl(7) code for more NULL pointer accesses, i came out
empty-handed; so this is just KNF and some code simplifications,
no functional change.

Modified Files:
--------------
    mdocml:
        tbl.c
        tbl_data.c
        tbl_html.c
        tbl_layout.c
        tbl_term.c

Revision Data
-------------
Index: tbl_data.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/tbl_data.c,v
retrieving revision 1.36
retrieving revision 1.37
diff -Ltbl_data.c -Ltbl_data.c -u -p -r1.36 -r1.37
--- tbl_data.c
+++ tbl_data.c
@@ -42,20 +42,16 @@ getdata(struct tbl_node *tbl, struct tbl
 {
 	struct tbl_dat	*dat;
 	struct tbl_cell	*cp;
-	int		 sv, spans;
+	int		 sv;
 
-	cp = NULL;
-	if (dp->last && dp->last->layout)
-		cp = dp->last->layout->next;
-	else if (NULL == dp->last)
-		cp = dp->layout->first;
+	cp = dp->last == NULL ? dp->layout->first : dp->last->layout->next;
 
 	/*
 	 * Skip over spanners, since
 	 * we want to match data with data layout cells in the header.
 	 */
 
-	while (cp && TBL_CELL_SPAN == cp->pos)
+	while (cp != NULL && cp->pos == TBL_CELL_SPAN)
 		cp = cp->next;
 
 	/*
@@ -63,7 +59,7 @@ getdata(struct tbl_node *tbl, struct tbl
 	 * cells.  This means that we have extra input.
 	 */
 
-	if (NULL == cp) {
+	if (cp == NULL) {
 		mandoc_msg(MANDOCERR_TBLDATA_EXTRA, tbl->parse,
 		    ln, *pos, p + *pos);
 		/* Skip to the end... */
@@ -72,25 +68,21 @@ getdata(struct tbl_node *tbl, struct tbl
 		return;
 	}
 
-	dat = mandoc_calloc(1, sizeof(struct tbl_dat));
+	dat = mandoc_calloc(1, sizeof(*dat));
 	dat->layout = cp;
 	dat->pos = TBL_DATA_NONE;
-
-	assert(TBL_CELL_SPAN != cp->pos);
-
-	for (spans = 0, cp = cp->next; cp; cp = cp->next)
-		if (TBL_CELL_SPAN == cp->pos)
-			spans++;
+	dat->spans = 0;
+	for (cp = cp->next; cp != NULL; cp = cp->next)
+		if (cp->pos == TBL_CELL_SPAN)
+			dat->spans++;
 		else
 			break;
 
-	dat->spans = spans;
-
-	if (dp->last) {
+	if (dp->last == NULL)
+		dp->first = dat;
+	else
 		dp->last->next = dat;
-		dp->last = dat;
-	} else
-		dp->last = dp->first = dat;
+	dp->last = dat;
 
 	sv = *pos;
 	while (p[*pos] && p[*pos] != tbl->opts.tab)
@@ -102,16 +94,12 @@ getdata(struct tbl_node *tbl, struct tbl
 	 * until a standalone `T}', are included in our cell.
 	 */
 
-	if (*pos - sv == 2 && 'T' == p[sv] && '{' == p[sv + 1]) {
+	if (*pos - sv == 2 && p[sv] == 'T' && p[sv + 1] == '{') {
 		tbl->part = TBL_PART_CDATA;
 		return;
 	}
 
-	assert(*pos - sv >= 0);
-
-	dat->string = mandoc_malloc((size_t)(*pos - sv + 1));
-	memcpy(dat->string, &p[sv], (size_t)(*pos - sv));
-	dat->string[*pos - sv] = '\0';
+	dat->string = mandoc_strndup(p + sv, *pos - sv);
 
 	if (p[*pos])
 		(*pos)++;
@@ -127,14 +115,12 @@ getdata(struct tbl_node *tbl, struct tbl
 	else
 		dat->pos = TBL_DATA_DATA;
 
-	if (TBL_CELL_HORIZ == dat->layout->pos ||
-	    TBL_CELL_DHORIZ == dat->layout->pos ||
-	    TBL_CELL_DOWN == dat->layout->pos)
-		if (TBL_DATA_DATA == dat->pos && '\0' != *dat->string)
-			mandoc_msg(MANDOCERR_TBLDATA_SPAN,
-			    tbl->parse, ln, sv, dat->string);
-
-	return;
+	if ((dat->layout->pos == TBL_CELL_HORIZ ||
+	    dat->layout->pos == TBL_CELL_DHORIZ ||
+	    dat->layout->pos == TBL_CELL_DOWN) &&
+	    dat->pos == TBL_DATA_DATA && *dat->string != '\0')
+		mandoc_msg(MANDOCERR_TBLDATA_SPAN,
+		    tbl->parse, ln, sv, dat->string);
 }
 
 int
@@ -152,7 +138,7 @@ tbl_cdata(struct tbl_node *tbl, int ln, 
 			pos++;
 			getdata(tbl, tbl->last_span, ln, p, &pos);
 			return(1);
-		} else if ('\0' == p[pos]) {
+		} else if (p[pos] == '\0') {
 			tbl->part = TBL_PART_DATA;
 			return(1);
 		}
@@ -162,7 +148,7 @@ tbl_cdata(struct tbl_node *tbl, int ln, 
 
 	dat->pos = TBL_DATA_DATA;
 
-	if (dat->string) {
+	if (dat->string != NULL) {
 		sz = strlen(p + pos) + strlen(dat->string) + 2;
 		dat->string = mandoc_realloc(dat->string, sz);
 		(void)strlcat(dat->string, " ", sz);
@@ -170,7 +156,7 @@ tbl_cdata(struct tbl_node *tbl, int ln, 
 	} else
 		dat->string = mandoc_strdup(p + pos);
 
-	if (TBL_CELL_DOWN == dat->layout->pos)
+	if (dat->layout->pos == TBL_CELL_DOWN)
 		mandoc_msg(MANDOCERR_TBLDATA_SPAN, tbl->parse,
 		    ln, pos, dat->string);
 
@@ -182,7 +168,7 @@ newspan(struct tbl_node *tbl, int line, 
 {
 	struct tbl_span	*dp;
 
-	dp = mandoc_calloc(1, sizeof(struct tbl_span));
+	dp = mandoc_calloc(1, sizeof(*dp));
 	dp->line = line;
 	dp->opts = &tbl->opts;
 	dp->layout = rp;
@@ -214,11 +200,11 @@ tbl_data(struct tbl_node *tbl, int ln, c
 	 * (it doesn't "consume" the layout).
 	 */
 
-	if (tbl->last_span) {
-		assert(tbl->last_span->layout);
+	if (tbl->last_span != NULL) {
 		if (tbl->last_span->pos == TBL_SPAN_DATA) {
 			for (rp = tbl->last_span->layout->next;
-					rp && rp->first; rp = rp->next) {
+			     rp != NULL && rp->first != NULL;
+			     rp = rp->next) {
 				switch (rp->first->pos) {
 				case TBL_CELL_HORIZ:
 					dp = newspan(tbl, ln, rp);
@@ -236,7 +222,7 @@ tbl_data(struct tbl_node *tbl, int ln, c
 		} else
 			rp = tbl->last_span->layout;
 
-		if (NULL == rp)
+		if (rp == NULL)
 			rp = tbl->last_span->layout;
 	} else
 		rp = tbl->first_row;
@@ -255,6 +241,6 @@ tbl_data(struct tbl_node *tbl, int ln, c
 
 	dp->pos = TBL_SPAN_DATA;
 
-	while ('\0' != p[pos])
+	while (p[pos] != '\0')
 		getdata(tbl, dp, ln, p, &pos);
 }
Index: tbl_html.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/tbl_html.c,v
retrieving revision 1.13
retrieving revision 1.14
diff -Ltbl_html.c -Ltbl_html.c -u -p -r1.13 -r1.14
--- tbl_html.c
+++ tbl_html.c
@@ -54,7 +54,7 @@ html_tblopen(struct html *h, const struc
 	struct roffsu	 su;
 	struct roffcol	*col;
 
-	if (TBL_SPAN_FIRST & sp->flags) {
+	if (sp->flags & TBL_SPAN_FIRST) {
 		h->tbl.len = html_tbl_len;
 		h->tbl.slen = html_tbl_strlen;
 		tblcalc(&h->tbl, sp, 0);
@@ -95,7 +95,7 @@ print_tbl(struct html *h, const struct t
 
 	/* Inhibit printing of spaces: we do padding ourselves. */
 
-	if (NULL == h->tblt)
+	if (h->tblt == NULL)
 		html_tblopen(h, sp);
 
 	assert(h->tblt);
@@ -118,10 +118,10 @@ print_tbl(struct html *h, const struct t
 			print_stagq(h, tt);
 			print_otag(h, TAG_TD, 0, NULL);
 
-			if (NULL == dp)
+			if (dp == NULL)
 				break;
-			if (TBL_CELL_DOWN != dp->layout->pos)
-				if (dp->string)
+			if (dp->layout->pos != TBL_CELL_DOWN)
+				if (dp->string != NULL)
 					print_text(h, dp->string);
 			dp = dp->next;
 		}
@@ -132,7 +132,7 @@ print_tbl(struct html *h, const struct t
 
 	h->flags &= ~HTML_NONOSPACE;
 
-	if (TBL_SPAN_LAST & sp->flags) {
+	if (sp->flags & TBL_SPAN_LAST) {
 		assert(h->tbl.cols);
 		free(h->tbl.cols);
 		h->tbl.cols = NULL;
Index: tbl.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/tbl.c,v
retrieving revision 1.36
retrieving revision 1.37
diff -Ltbl.c -Ltbl.c -u -p -r1.36 -r1.37
--- tbl.c
+++ tbl.c
@@ -91,7 +91,7 @@ tbl_alloc(int pos, int line, struct mpar
 {
 	struct tbl_node	*tbl;
 
-	tbl = mandoc_calloc(1, sizeof(struct tbl_node));
+	tbl = mandoc_calloc(1, sizeof(*tbl));
 	tbl->line = line;
 	tbl->pos = pos;
 	tbl->parse = parse;
@@ -110,9 +110,9 @@ tbl_free(struct tbl_node *tbl)
 	struct tbl_dat	*dp;
 	struct tbl_head	*hp;
 
-	while (NULL != (rp = tbl->first_row)) {
+	while ((rp = tbl->first_row) != NULL) {
 		tbl->first_row = rp->next;
-		while (rp->first) {
+		while (rp->first != NULL) {
 			cp = rp->first;
 			rp->first = cp->next;
 			free(cp);
@@ -120,19 +120,18 @@ tbl_free(struct tbl_node *tbl)
 		free(rp);
 	}
 
-	while (NULL != (sp = tbl->first_span)) {
+	while ((sp = tbl->first_span) != NULL) {
 		tbl->first_span = sp->next;
-		while (sp->first) {
+		while (sp->first != NULL) {
 			dp = sp->first;
 			sp->first = dp->next;
-			if (dp->string)
-				free(dp->string);
+			free(dp->string);
 			free(dp);
 		}
 		free(sp);
 	}
 
-	while (NULL != (hp = tbl->first_head)) {
+	while ((hp = tbl->first_head) != NULL) {
 		tbl->first_head = hp->next;
 		free(hp);
 	}
Index: tbl_layout.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/tbl_layout.c,v
retrieving revision 1.35
retrieving revision 1.36
diff -Ltbl_layout.c -Ltbl_layout.c -u -p -r1.35 -r1.36
--- tbl_layout.c
+++ tbl_layout.c
@@ -326,9 +326,9 @@ cell_alloc(struct tbl_node *tbl, struct 
 	struct tbl_cell	*p, *pp;
 	struct tbl_head	*h, *hp;
 
-	p = mandoc_calloc(1, sizeof(struct tbl_cell));
+	p = mandoc_calloc(1, sizeof(*p));
 
-	if (NULL != (pp = rp->last)) {
+	if ((pp = rp->last) != NULL) {
 		pp->next = p;
 		h = pp->head->next;
 	} else {
@@ -341,15 +341,15 @@ cell_alloc(struct tbl_node *tbl, struct 
 
 	/* Re-use header. */
 
-	if (h) {
+	if (h != NULL) {
 		p->head = h;
 		return(p);
 	}
 
-	hp = mandoc_calloc(1, sizeof(struct tbl_head));
+	hp = mandoc_calloc(1, sizeof(*hp));
 	hp->ident = tbl->opts.cols++;
 
-	if (tbl->last_head) {
+	if (tbl->last_head != NULL) {
 		hp->prev = tbl->last_head;
 		tbl->last_head->next = hp;
 	} else
Index: tbl_term.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/tbl_term.c,v
retrieving revision 1.34
retrieving revision 1.35
diff -Ltbl_term.c -Ltbl_term.c -u -p -r1.34 -r1.35
--- tbl_term.c
+++ tbl_term.c
@@ -262,11 +262,10 @@ tbl_data(struct termp *tp, const struct 
 	const struct roffcol *col)
 {
 
-	if (NULL == dp) {
+	if (dp == NULL) {
 		tbl_char(tp, ASCII_NBRSP, col->width);
 		return;
 	}
-	assert(dp->layout);
 
 	switch (dp->pos) {
 	case TBL_DATA_NONE:
@@ -396,8 +395,7 @@ tbl_number(struct termp *tp, const struc
 
 	psz = term_strlen(tp, buf);
 
-	if (NULL != (cp = strrchr(dp->string, opts->decimal))) {
-		buf[1] = '\0';
+	if ((cp = strrchr(dp->string, opts->decimal)) != NULL) {
 		for (ssz = 0, i = 0; cp != &dp->string[i]; i++) {
 			buf[0] = dp->string[i];
 			ssz += term_strlen(tp, buf);
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2015-01-30  2:09 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-30  2:09 mdocml: Auditing the tbl(7) code for more NULL pointer accesses, i came schwarze

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).