Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/indent indent: fix Clang-Tidy warnings, clean up bak...



details:   https://anonhg.NetBSD.org/src/rev/01386a242bda
branches:  trunk
changeset: 988236:01386a242bda
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue Oct 05 06:55:24 2021 +0000

description:
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).

diffstat:

 usr.bin/indent/indent.c |  41 +++++++++++++++++------------------------
 usr.bin/indent/parse.c  |  12 ++++++------
 2 files changed, 23 insertions(+), 30 deletions(-)

diffs (142 lines):

diff -r aa95ac982e0b -r 01386a242bda usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Tue Oct 05 06:49:19 2021 +0000
+++ b/usr.bin/indent/indent.c   Tue Oct 05 06:55:24 2021 +0000
@@ -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
@@ -43,7 +43,7 @@
 
 #include <sys/cdefs.h>
 #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__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -607,7 +607,6 @@
 static void
 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)) {
        diag(0, "Reached internal limit of %zu unclosed parens",
            nitems(ps.paren_indents));
@@ -625,13 +624,13 @@
     *code.e++ = token.s[0];
 
     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",
        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
            && 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]);
     }
     if (ps.in_or_st && *token.s == '(' && ps.tos <= 2) {
@@ -733,7 +732,7 @@
 process_question(int *inout_squest)
 {
     (*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 */
     if (ps.want_blank)
        *code.e++ = ' ';
@@ -1425,37 +1424,31 @@
 }
 
 /*
- * copy input file to backup file if in_name is /blah/blah/blah/file, then
- * backup file will be ".Bfile" then make the backup file the input and
- * original input file the output
+ * Copy the input file to the backup file, then make the backup file the input
+ * and the original input file the output.
  */
 static void
 bakcopy(void)
 {
     ssize_t n;
-    int bakchn;
+    int bak_fd;
     char buff[8 * 1024];
-    const char *p;
 
-    /* construct file name .Bfile */
-    for (p = in_name; *p != '\0'; p++);        /* skip to end of string */
-    while (p > in_name && *p != '/')   /* find last '/' */
-       p--;
-    if (*p == '/')
-       p++;
-    sprintf(bakfile, "%s%s", p, backup_suffix);
+    const char *last_slash = strrchr(in_name, '/');
+    snprintf(bakfile, sizeof(bakfile), "%s%s",
+       last_slash != NULL ? last_slash + 1 : in_name, backup_suffix);
 
     /* copy in_name to backup file */
-    bakchn = creat(bakfile, 0600);
-    if (bakchn < 0)
+    bak_fd = creat(bakfile, 0600);
+    if (bak_fd < 0)
        err(1, "%s", bakfile);
     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);
     if (n < 0)
        err(1, "%s", in_name);
-    close(bakchn);
-    fclose(input);
+    close(bak_fd);
+    (void)fclose(input);
 
     /* re-open backup file as the input file */
     input = fopen(bakfile, "r");
@@ -1492,7 +1485,7 @@
            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) {
        *code.e++ = ' ';
        pos++;
diff -r aa95ac982e0b -r 01386a242bda usr.bin/indent/parse.c
--- a/usr.bin/indent/parse.c    Tue Oct 05 06:49:19 2021 +0000
+++ b/usr.bin/indent/parse.c    Tue Oct 05 06:55:24 2021 +0000
@@ -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
@@ -189,10 +189,10 @@
        ps.cstk[ps.tos] = case_ind;
        /* save current case indent level */
        ps.il[ps.tos] = ps.ind_level_follow;
-       case_ind = ps.ind_level_follow + opt.case_indent; /* cases should be
-                                * one level deeper than the switch */
-       ps.ind_level_follow += opt.case_indent + 1; /* statements should be
-                                * two levels deeper */
+       /* cases should be one level deeper than the switch */
+       case_ind = (float)ps.ind_level_follow + opt.case_indent;
+       /* statements should be two levels deeper */
+       ps.ind_level_follow += (int)opt.case_indent + 1;
        ps.search_brace = opt.btype_2;
        break;
 
@@ -258,7 +258,7 @@
        ps.ind_level_follow = ps.il[i];
        /*
         * 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
         */
        return true;



Home | Main Index | Thread Index | Old Index