indent: fix Clang-Tidy warnings, clean up bakcopy

The comment above and inside bakcopy had been outdated for at least the
last 28 years, the backup file is named "%s.BAK", not ".B%s".

Prevent buffer overflow for very long filenames (sprintf -> snprintf).
This commit is contained in:
rillig 2021-10-05 06:55:24 +00:00
parent ed959c6f7e
commit 54a4dc048b
2 changed files with 23 additions and 30 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: indent.c,v 1.102 2021/10/05 06:49:19 rillig Exp $ */ /* $NetBSD: indent.c,v 1.103 2021/10/05 06:55:24 rillig Exp $ */
/*- /*-
* SPDX-License-Identifier: BSD-4-Clause * SPDX-License-Identifier: BSD-4-Clause
@ -43,7 +43,7 @@ static char sccsid[] = "@(#)indent.c 5.17 (Berkeley) 6/7/93";
#include <sys/cdefs.h> #include <sys/cdefs.h>
#if defined(__NetBSD__) #if defined(__NetBSD__)
__RCSID("$NetBSD: indent.c,v 1.102 2021/10/05 06:49:19 rillig Exp $"); __RCSID("$NetBSD: indent.c,v 1.103 2021/10/05 06:55:24 rillig Exp $");
#elif defined(__FreeBSD__) #elif defined(__FreeBSD__)
__FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $"); __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
#endif #endif
@ -607,7 +607,6 @@ want_blank_before_lparen(void)
static void static void
process_lparen_or_lbracket(int dec_ind, bool tabs_to_var, bool sp_sw) process_lparen_or_lbracket(int dec_ind, bool tabs_to_var, bool sp_sw)
{ {
/* count parens to make Healy happy */
if (++ps.p_l_follow == nitems(ps.paren_indents)) { if (++ps.p_l_follow == nitems(ps.paren_indents)) {
diag(0, "Reached internal limit of %zu unclosed parens", diag(0, "Reached internal limit of %zu unclosed parens",
nitems(ps.paren_indents)); nitems(ps.paren_indents));
@ -625,13 +624,13 @@ process_lparen_or_lbracket(int dec_ind, bool tabs_to_var, bool sp_sw)
*code.e++ = token.s[0]; *code.e++ = token.s[0];
ps.paren_indents[ps.p_l_follow - 1] = ps.paren_indents[ps.p_l_follow - 1] =
indentation_after_range(0, code.s, code.e); (short)indentation_after_range(0, code.s, code.e);
debug_println("paren_indent[%d] is now %d", debug_println("paren_indent[%d] is now %d",
ps.p_l_follow - 1, ps.paren_indents[ps.p_l_follow - 1]); ps.p_l_follow - 1, ps.paren_indents[ps.p_l_follow - 1]);
if (sp_sw && ps.p_l_follow == 1 && opt.extra_expression_indent if (sp_sw && ps.p_l_follow == 1 && opt.extra_expression_indent
&& ps.paren_indents[0] < 2 * opt.indent_size) { && ps.paren_indents[0] < 2 * opt.indent_size) {
ps.paren_indents[0] = 2 * opt.indent_size; ps.paren_indents[0] = (short)(2 * opt.indent_size);
debug_println("paren_indent[0] is now %d", ps.paren_indents[0]); debug_println("paren_indent[0] is now %d", ps.paren_indents[0]);
} }
if (ps.in_or_st && *token.s == '(' && ps.tos <= 2) { if (ps.in_or_st && *token.s == '(' && ps.tos <= 2) {
@ -733,7 +732,7 @@ static void
process_question(int *inout_squest) process_question(int *inout_squest)
{ {
(*inout_squest)++; /* this will be used when a later colon (*inout_squest)++; /* this will be used when a later colon
* appears so we can distinguish the * appears, so we can distinguish the
* <c>?<n>:<n> construct */ * <c>?<n>:<n> construct */
if (ps.want_blank) if (ps.want_blank)
*code.e++ = ' '; *code.e++ = ' ';
@ -1425,37 +1424,31 @@ main(int argc, char **argv)
} }
/* /*
* copy input file to backup file if in_name is /blah/blah/blah/file, then * Copy the input file to the backup file, then make the backup file the input
* backup file will be ".Bfile" then make the backup file the input and * and the original input file the output.
* original input file the output
*/ */
static void static void
bakcopy(void) bakcopy(void)
{ {
ssize_t n; ssize_t n;
int bakchn; int bak_fd;
char buff[8 * 1024]; char buff[8 * 1024];
const char *p;
/* construct file name .Bfile */ const char *last_slash = strrchr(in_name, '/');
for (p = in_name; *p != '\0'; p++); /* skip to end of string */ snprintf(bakfile, sizeof(bakfile), "%s%s",
while (p > in_name && *p != '/') /* find last '/' */ last_slash != NULL ? last_slash + 1 : in_name, backup_suffix);
p--;
if (*p == '/')
p++;
sprintf(bakfile, "%s%s", p, backup_suffix);
/* copy in_name to backup file */ /* copy in_name to backup file */
bakchn = creat(bakfile, 0600); bak_fd = creat(bakfile, 0600);
if (bakchn < 0) if (bak_fd < 0)
err(1, "%s", bakfile); err(1, "%s", bakfile);
while ((n = read(fileno(input), buff, sizeof(buff))) > 0) while ((n = read(fileno(input), buff, sizeof(buff))) > 0)
if (write(bakchn, buff, (size_t)n) != n) if (write(bak_fd, buff, (size_t)n) != n)
err(1, "%s", bakfile); err(1, "%s", bakfile);
if (n < 0) if (n < 0)
err(1, "%s", in_name); err(1, "%s", in_name);
close(bakchn); close(bak_fd);
fclose(input); (void)fclose(input);
/* re-open backup file as the input file */ /* re-open backup file as the input file */
input = fopen(bakfile, "r"); input = fopen(bakfile, "r");
@ -1492,7 +1485,7 @@ indent_declaration(int cur_dec_ind, bool tabs_to_var)
pos = tpos; pos = tpos;
} }
} }
check_size_code((size_t)(cur_dec_ind - pos + 1)); check_size_code((size_t)(cur_dec_ind - pos) + 1);
while (pos < cur_dec_ind) { while (pos < cur_dec_ind) {
*code.e++ = ' '; *code.e++ = ' ';
pos++; pos++;

View File

@ -1,4 +1,4 @@
/* $NetBSD: parse.c,v 1.28 2021/10/05 06:24:06 rillig Exp $ */ /* $NetBSD: parse.c,v 1.29 2021/10/05 06:55:24 rillig Exp $ */
/*- /*-
* SPDX-License-Identifier: BSD-4-Clause * SPDX-License-Identifier: BSD-4-Clause
@ -189,10 +189,10 @@ parse(token_type ttype)
ps.cstk[ps.tos] = case_ind; ps.cstk[ps.tos] = case_ind;
/* save current case indent level */ /* save current case indent level */
ps.il[ps.tos] = ps.ind_level_follow; ps.il[ps.tos] = ps.ind_level_follow;
case_ind = ps.ind_level_follow + opt.case_indent; /* cases should be /* cases should be one level deeper than the switch */
* one level deeper than the switch */ case_ind = (float)ps.ind_level_follow + opt.case_indent;
ps.ind_level_follow += opt.case_indent + 1; /* statements should be /* statements should be two levels deeper */
* two levels deeper */ ps.ind_level_follow += (int)opt.case_indent + 1;
ps.search_brace = opt.btype_2; ps.search_brace = opt.btype_2;
break; break;
@ -258,7 +258,7 @@ reduce_stmt(void)
ps.ind_level_follow = ps.il[i]; ps.ind_level_follow = ps.il[i];
/* /*
* for the time being, we will assume that there is no else on this * for the time being, we will assume that there is no else on this
* if, and set the indentation level accordingly. If an else is * if, and set the indentation level accordingly. If an 'else' is
* scanned, it will be fixed up later * scanned, it will be fixed up later
*/ */
return true; return true;