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: make off-by-one error in main_prepare...



details:   https://anonhg.NetBSD.org/src/rev/1e3cbfff494b
branches:  trunk
changeset: 1023967:1e3cbfff494b
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue Oct 05 17:12:20 2021 +0000

description:
indent: make off-by-one error in main_prepare_parsing more visible

No functional change.

diffstat:

 tests/usr.bin/indent/ps_ind_level.0 |   4 ++--
 usr.bin/indent/indent.c             |  14 +++++++-------
 2 files changed, 9 insertions(+), 9 deletions(-)

diffs (59 lines):

diff -r 64c615f76afc -r 1e3cbfff494b tests/usr.bin/indent/ps_ind_level.0
--- a/tests/usr.bin/indent/ps_ind_level.0       Tue Oct 05 17:09:45 2021 +0000
+++ b/tests/usr.bin/indent/ps_ind_level.0       Tue Oct 05 17:12:20 2021 +0000
@@ -1,10 +1,10 @@
-/* $NetBSD: ps_ind_level.0,v 1.1 2021/10/05 17:09:45 rillig Exp $ */
+/* $NetBSD: ps_ind_level.0,v 1.2 2021/10/05 17:12:20 rillig Exp $ */
 /* $FreeBSD$ */
 /* $ */
 /* $ The leading indentation in the first line determines the indentation */
 /* $ of the remaining code. In this particular test, the indentation is set */
 /* $ to 5 and the tabulator is set to 8, to demonstrate an off-by-one error */
-/* $ in main_prepare_parsing that has been fixed in indent.c 1.106 from */
+/* $ in main_prepare_parsing that has been fixed in indent.c 1.107 from */
 /* $ 2021-10-05. */
 /* $ */
 /* $ The declaration in the below line is indented by 3 tabs, amounting to */
diff -r 64c615f76afc -r 1e3cbfff494b usr.bin/indent/indent.c
--- a/usr.bin/indent/indent.c   Tue Oct 05 17:09:45 2021 +0000
+++ b/usr.bin/indent/indent.c   Tue Oct 05 17:12:20 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: indent.c,v 1.105 2021/10/05 16:33:25 rillig Exp $      */
+/*     $NetBSD: indent.c,v 1.106 2021/10/05 17:12:20 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.105 2021/10/05 16:33:25 rillig Exp $");
+__RCSID("$NetBSD: indent.c,v 1.106 2021/10/05 17:12:20 rillig Exp $");
 #elif defined(__FreeBSD__)
 __FBSDID("$FreeBSD: head/usr.bin/indent/indent.c 340138 2018-11-04 19:24:49Z oshogbo $");
 #endif
@@ -501,19 +501,19 @@
     parse(semicolon);
 
     char *p = buf_ptr;
-    int col = 1;
+    int ind = 0;
 
     for (;;) {
        if (*p == ' ')
-           col++;
+           ind++;
        else if (*p == '\t')
-           col = opt.tabsize * (1 + (col - 1) / opt.tabsize) + 1;
+           ind = opt.tabsize * (1 + ind / opt.tabsize);
        else
            break;
        p++;
     }
-    if (col > opt.indent_size)
-       ps.ind_level = ps.ind_level_follow = col / opt.indent_size;
+    if (ind >= opt.indent_size)
+       ps.ind_level = ps.ind_level_follow = (ind + 1) / opt.indent_size;
 }
 
 static void __attribute__((__noreturn__))



Home | Main Index | Thread Index | Old Index