]> Sergey Matveev's repositories - st.git/commitdiff
Fix bounds checks of dc.col
authorPeter Hofmann <scm@uninformativ.de>
Sat, 7 Oct 2023 05:39:00 +0000 (07:39 +0200)
committerHiltjo Posthuma <hiltjo@codemadness.org>
Sat, 7 Oct 2023 10:16:59 +0000 (12:16 +0200)
dc.collen is the length of dc.col, not the maximum index, hence if x is
equal to dc.collen, then it's an error.

With config.def.h, the last valid index is 259, so this correctly
reports "black":

    $ printf '\033]4;259;?\e\\'

260 is an invalid index and this reports garbage instead of printing an
error:

    $ printf '\033]4;260;?\e\\'

x.c

diff --git a/x.c b/x.c
index aa0999708da84930a9752da8dd6d5bdc9991bb5d..b36fb8c8f0ce6272d59224ac28d2b92333128507 100644 (file)
--- a/x.c
+++ b/x.c
@@ -818,7 +818,7 @@ xloadcols(void)
 int
 xgetcolor(int x, unsigned char *r, unsigned char *g, unsigned char *b)
 {
-       if (!BETWEEN(x, 0, dc.collen))
+       if (!BETWEEN(x, 0, dc.collen - 1))
                return 1;
 
        *r = dc.col[x].color.red >> 8;
@@ -833,7 +833,7 @@ xsetcolorname(int x, const char *name)
 {
        Color ncolor;
 
-       if (!BETWEEN(x, 0, dc.collen))
+       if (!BETWEEN(x, 0, dc.collen - 1))
                return 1;
 
        if (!xloadcolor(x, name, &ncolor))