Increase XmbLookupString buffer
[st.git] / st.c
diff --git a/st.c b/st.c
index ede7ae6..3e48410 100644 (file)
--- a/st.c
+++ b/st.c
@@ -135,7 +135,7 @@ typedef struct {
 /* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
 typedef struct {
        char buf[ESC_BUF_SIZ]; /* raw string */
-       int len;               /* raw string length */
+       size_t len;            /* raw string length */
        char priv;
        int arg[ESC_ARG_SIZ];
        int narg;              /* nb of args */
@@ -146,8 +146,9 @@ typedef struct {
 /* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
 typedef struct {
        char type;             /* ESC type ... */
-       char buf[STR_BUF_SIZ]; /* raw string */
-       int len;               /* raw string length */
+       char *buf;             /* allocated raw string */
+       size_t siz;            /* allocation size */
+       size_t len;            /* raw string length */
        char *args[STR_ARG_SIZ];
        int narg;              /* nb of args */
 } STREscape;
@@ -366,7 +367,7 @@ char
 base64dec_getc(const char **src)
 {
        while (**src && !isprint(**src)) (*src)++;
-       return *((*src)++);
+       return **src ? *((*src)++) : '=';  /* emulate padding if string ends */
 }
 
 char *
@@ -384,6 +385,10 @@ base64dec(const char *src)
                int c = base64_digits[(unsigned char) base64dec_getc(&src)];
                int d = base64_digits[(unsigned char) base64dec_getc(&src)];
 
+               /* invalid input. 'a' can be -1, e.g. if src is "\n" (c-str) */
+               if (a == -1 || b == -1)
+                       break;
+
                *dst++ = (a << 2) | ((b & 0x30) >> 4);
                if (c == -1)
                        break;
@@ -1799,7 +1804,7 @@ csihandle(void)
 void
 csidump(void)
 {
-       int i;
+       size_t i;
        uint c;
 
        fprintf(stderr, "ESC[");
@@ -1917,7 +1922,7 @@ strparse(void)
 void
 strdump(void)
 {
-       int i;
+       size_t i;
        uint c;
 
        fprintf(stderr, "ESC%c", strescseq.type);
@@ -1944,7 +1949,10 @@ strdump(void)
 void
 strreset(void)
 {
-       memset(&strescseq, 0, sizeof(strescseq));
+       strescseq = (STREscape){
+               .buf = xrealloc(strescseq.buf, STR_BUF_SIZ),
+               .siz = STR_BUF_SIZ,
+       };
 }
 
 void
@@ -2326,7 +2334,7 @@ tputc(Rune u)
                if (term.esc&ESC_DCS && strescseq.len == 0 && u == 'q')
                        term.mode |= MODE_SIXEL;
 
-               if (strescseq.len+len >= sizeof(strescseq.buf)-1) {
+               if (strescseq.len+len >= strescseq.siz) {
                        /*
                         * Here is a bug in terminals. If the user never sends
                         * some code to stop the str or esc command, then st
@@ -2340,7 +2348,10 @@ tputc(Rune u)
                         * term.esc = 0;
                         * strhandle();
                         */
-                       return;
+                       if (strescseq.siz > (SIZE_MAX - UTF_SIZ) / 2)
+                               return;
+                       strescseq.siz *= 2;
+                       strescseq.buf = xrealloc(strescseq.buf, strescseq.siz);
                }
 
                memmove(&strescseq.buf[strescseq.len], c, len);