kdb: Cleanup math with KDB_CMD_HISTORY_COUNT
authorDouglas Anderson <dianders@chromium.org>
Thu, 7 May 2020 23:11:46 +0000 (16:11 -0700)
committerDaniel Thompson <daniel.thompson@linaro.org>
Tue, 2 Jun 2020 14:15:46 +0000 (15:15 +0100)
From code inspection the math in handle_ctrl_cmd() looks super sketchy
because it subjects -1 from cmdptr and then does a "%
KDB_CMD_HISTORY_COUNT".  It turns out that this code works because
"cmdptr" is unsigned and KDB_CMD_HISTORY_COUNT is a nice power of 2.
Let's make this a little less sketchy.

This patch should be a no-op.

Signed-off-by: Douglas Anderson <dianders@chromium.org>
Link: https://lore.kernel.org/r/20200507161125.1.I2cce9ac66e141230c3644b8174b6c15d4e769232@changeid
Reviewed-by: Sumit Garg <sumit.garg@linaro.org>
Signed-off-by: Daniel Thompson <daniel.thompson@linaro.org>
kernel/debug/kdb/kdb_main.c

index 515379cbf20925caf95f43dbc88086bc812a6542..6865a0f58d3831dee701b38cb577a8c137c5c624 100644 (file)
@@ -1108,7 +1108,8 @@ static int handle_ctrl_cmd(char *cmd)
        switch (*cmd) {
        case CTRL_P:
                if (cmdptr != cmd_tail)
-                       cmdptr = (cmdptr-1) % KDB_CMD_HISTORY_COUNT;
+                       cmdptr = (cmdptr + KDB_CMD_HISTORY_COUNT - 1) %
+                                KDB_CMD_HISTORY_COUNT;
                strscpy(cmd_cur, cmd_hist[cmdptr], CMD_BUFLEN);
                return 1;
        case CTRL_N: