diff options
author | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2015-01-30 02:08:38 +0000 |
---|---|---|
committer | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2015-01-30 02:08:38 +0000 |
commit | d04b6093e3c4e5d042715feb56796f9767e1c70e (patch) | |
tree | f96846e8c990e8d2d8433fe72a81544497e16b02 /usr.bin/mandoc | |
parent | 9a738b89a5f6d79df3a1193c1e448f3da3b6bd25 (diff) |
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.
Diffstat (limited to 'usr.bin/mandoc')
-rw-r--r-- | usr.bin/mandoc/tbl.c | 17 | ||||
-rw-r--r-- | usr.bin/mandoc/tbl_data.c | 76 | ||||
-rw-r--r-- | usr.bin/mandoc/tbl_html.c | 14 | ||||
-rw-r--r-- | usr.bin/mandoc/tbl_layout.c | 12 | ||||
-rw-r--r-- | usr.bin/mandoc/tbl_term.c | 8 |
5 files changed, 55 insertions, 72 deletions
diff --git a/usr.bin/mandoc/tbl.c b/usr.bin/mandoc/tbl.c index 8abee886e5f..e5af6b3efa4 100644 --- a/usr.bin/mandoc/tbl.c +++ b/usr.bin/mandoc/tbl.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tbl.c,v 1.17 2015/01/28 17:30:37 schwarze Exp $ */ +/* $OpenBSD: tbl.c,v 1.18 2015/01/30 02:08:37 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> * Copyright (c) 2011, 2015 Ingo Schwarze <schwarze@openbsd.org> @@ -89,7 +89,7 @@ tbl_alloc(int pos, int line, struct mparse *parse) { 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; @@ -108,9 +108,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); @@ -118,19 +118,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); } diff --git a/usr.bin/mandoc/tbl_data.c b/usr.bin/mandoc/tbl_data.c index b4d7f33ed75..710f3105011 100644 --- a/usr.bin/mandoc/tbl_data.c +++ b/usr.bin/mandoc/tbl_data.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tbl_data.c,v 1.23 2015/01/28 17:30:37 schwarze Exp $ */ +/* $OpenBSD: tbl_data.c,v 1.24 2015/01/30 02:08:37 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> * Copyright (c) 2011, 2015 Ingo Schwarze <schwarze@openbsd.org> @@ -40,20 +40,16 @@ getdata(struct tbl_node *tbl, struct tbl_span *dp, { 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; /* @@ -61,7 +57,7 @@ getdata(struct tbl_node *tbl, struct tbl_span *dp, * 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... */ @@ -70,25 +66,21 @@ getdata(struct tbl_node *tbl, struct tbl_span *dp, 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) @@ -100,16 +92,12 @@ getdata(struct tbl_node *tbl, struct tbl_span *dp, * 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)++; @@ -125,14 +113,12 @@ getdata(struct tbl_node *tbl, struct tbl_span *dp, 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 @@ -150,7 +136,7 @@ tbl_cdata(struct tbl_node *tbl, int ln, const char *p, int pos) 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); } @@ -160,7 +146,7 @@ tbl_cdata(struct tbl_node *tbl, int ln, const char *p, int pos) 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); @@ -168,7 +154,7 @@ tbl_cdata(struct tbl_node *tbl, int ln, const char *p, int pos) } 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); @@ -180,7 +166,7 @@ newspan(struct tbl_node *tbl, int line, struct tbl_row *rp) { 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; @@ -212,11 +198,11 @@ tbl_data(struct tbl_node *tbl, int ln, const char *p, int pos) * (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); @@ -234,7 +220,7 @@ tbl_data(struct tbl_node *tbl, int ln, const char *p, int pos) } else rp = tbl->last_span->layout; - if (NULL == rp) + if (rp == NULL) rp = tbl->last_span->layout; } else rp = tbl->first_row; @@ -253,6 +239,6 @@ tbl_data(struct tbl_node *tbl, int ln, const char *p, int pos) dp->pos = TBL_SPAN_DATA; - while ('\0' != p[pos]) + while (p[pos] != '\0') getdata(tbl, dp, ln, p, &pos); } diff --git a/usr.bin/mandoc/tbl_html.c b/usr.bin/mandoc/tbl_html.c index 34382121c65..272352683de 100644 --- a/usr.bin/mandoc/tbl_html.c +++ b/usr.bin/mandoc/tbl_html.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tbl_html.c,v 1.8 2014/10/14 02:16:02 schwarze Exp $ */ +/* $OpenBSD: tbl_html.c,v 1.9 2015/01/30 02:08:37 schwarze Exp $ */ /* * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv> * @@ -52,7 +52,7 @@ html_tblopen(struct html *h, const struct tbl_span *sp) 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); @@ -93,7 +93,7 @@ print_tbl(struct html *h, const struct tbl_span *sp) /* Inhibit printing of spaces: we do padding ourselves. */ - if (NULL == h->tblt) + if (h->tblt == NULL) html_tblopen(h, sp); assert(h->tblt); @@ -116,10 +116,10 @@ print_tbl(struct html *h, const struct tbl_span *sp) 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; } @@ -130,7 +130,7 @@ print_tbl(struct html *h, const struct tbl_span *sp) 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; diff --git a/usr.bin/mandoc/tbl_layout.c b/usr.bin/mandoc/tbl_layout.c index 250c00d00c4..5d4057a341e 100644 --- a/usr.bin/mandoc/tbl_layout.c +++ b/usr.bin/mandoc/tbl_layout.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tbl_layout.c,v 1.22 2015/01/30 00:27:09 schwarze Exp $ */ +/* $OpenBSD: tbl_layout.c,v 1.23 2015/01/30 02:08:37 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv> * Copyright (c) 2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org> @@ -324,9 +324,9 @@ cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos) 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 { @@ -339,15 +339,15 @@ cell_alloc(struct tbl_node *tbl, struct tbl_row *rp, enum tbl_cellt pos) /* 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 diff --git a/usr.bin/mandoc/tbl_term.c b/usr.bin/mandoc/tbl_term.c index 5dd8384c6e4..07ace637ec6 100644 --- a/usr.bin/mandoc/tbl_term.c +++ b/usr.bin/mandoc/tbl_term.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tbl_term.c,v 1.22 2015/01/28 04:18:31 schwarze Exp $ */ +/* $OpenBSD: tbl_term.c,v 1.23 2015/01/30 02:08:37 schwarze Exp $ */ /* * Copyright (c) 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv> * Copyright (c) 2011, 2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org> @@ -260,11 +260,10 @@ tbl_data(struct termp *tp, const struct tbl_opts *opts, 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: @@ -394,8 +393,7 @@ tbl_number(struct termp *tp, const struct tbl_opts *opts, 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); |