Fix free of $value after ${run...}
[exim.git] / src / src / expand.c
index 3428179d8790c50284ccf3c560a66f7a5e4e00eb..e0c571ade62cf194490a739e3e25eb5389bbb4ce 100644 (file)
@@ -2037,7 +2037,8 @@ switch (vp->type)
     if (!*ss && deliver_datafile >= 0)  /* Read body when needed */
       {
       uschar * body;
-      off_t start_offset = SPOOL_DATA_START_OFFSET;
+      off_t start_offset_o = spool_data_start_offset(message_id);
+      off_t start_offset = start_offset_o;
       int len = message_body_visible;
 
       if (len > message_size) len = message_size;
@@ -2049,8 +2050,8 @@ switch (vp->type)
        if (fstat(deliver_datafile, &statbuf) == 0)
          {
          start_offset = statbuf.st_size - len;
-         if (start_offset < SPOOL_DATA_START_OFFSET)
-           start_offset = SPOOL_DATA_START_OFFSET;
+         if (start_offset < start_offset_o)
+           start_offset = start_offset_o;
          }
        }
       if (lseek(deliver_datafile, start_offset, SEEK_SET) < 0)
@@ -2383,19 +2384,26 @@ static uschar *
 json_nextinlist(const uschar ** list)
 {
 unsigned array_depth = 0, object_depth = 0;
+BOOL quoted = FALSE;
 const uschar * s = *list, * item;
 
 skip_whitespace(&s);
 
 for (item = s;
-     *s && (*s != ',' || array_depth != 0 || object_depth != 0);
+     *s && (*s != ',' || array_depth != 0 || object_depth != 0 || quoted);
      s++)
-  switch (*s)
+  if (!quoted) switch (*s)
     {
     case '[': array_depth++; break;
     case ']': array_depth--; break;
     case '{': object_depth++; break;
     case '}': object_depth--; break;
+    case '"': quoted = TRUE;
+    }
+  else switch(*s)
+    {
+    case '\\': s++; break;             /* backslash protects one char */
+    case '"':  quoted = FALSE; break;
     }
 *list = *s ? s+1 : s;
 if (item == s) return NULL;
@@ -3956,10 +3964,9 @@ if (Ustrlen(key) > 64)
 hash_source = string_catn(NULL, key_num, 1);
 hash_source = string_catn(hash_source, daystamp, 3);
 hash_source = string_cat(hash_source, address);
-(void) string_from_gstring(hash_source);
 
 DEBUG(D_expand)
-  debug_printf_indent("prvs: hash source is '%s'\n", hash_source->s);
+  debug_printf_indent("prvs: hash source is '%Y'\n", hash_source);
 
 memset(innerkey, 0x36, 64);
 memset(outerkey, 0x5c, 64);
@@ -5617,6 +5624,8 @@ while (*s)
       FILE * f;
       const uschar * arg, ** argv;
       BOOL late_expand = TRUE;
+      uschar * save_value = lookup_value;
+      int yesno;
 
       if (expand_forbid & RDO_RUN)
         {
@@ -5740,20 +5749,24 @@ while (*s)
             expand_string_message = string_sprintf("command killed by signal %d",
               -runrc);
 
+         lookup_value = save_value;
           goto EXPAND_FAILED;
           }
         }
 
       /* Process the yes/no strings; $value may be useful in both cases */
 
-      switch(process_yesno(
+      yesno = process_yesno(
                flags,                  /* were previously skipping */
                runrc == 0,             /* success/failure indicator */
                lookup_value,           /* value to reset for string2 */
                &s,                     /* input pointer */
                &yield,                 /* output pointer */
                US"run",                        /* condition type */
-              &resetok))
+              &resetok);
+      lookup_value = save_value;
+
+      switch(yesno)
         {
         case 1: goto EXPAND_FAILED;          /* when all is well, the */
         case 2: goto EXPAND_FAILED_CURLY;    /* returned value is 0 */
@@ -7047,6 +7060,7 @@ while (*s)
         case 2:
         case 3: goto EXPAND_FAILED;
         }
+      if (flags & ESI_SKIPPING) continue;
 
       if (sub[1] && *(sub[1]))
        {
@@ -7263,7 +7277,7 @@ NOT_ITEM: ;
            "operator is \"%s\", which is not a decimal number", sub);
          goto EXPAND_FAILED;
          }
-       yield = string_cat(yield, string_base62(n));
+       yield = string_cat(yield, string_base62_32(n));         /*XXX only handles 32b input range.  Need variants? */
        break;
        }
 
@@ -7854,7 +7868,7 @@ NOT_ITEM: ;
        case EOP_UTF8CLEAN:
          {
          int seq_len = 0, index = 0, bytes_left = 0, complete;
-         long codepoint = -1;
+         u_long codepoint = (u_long)-1;
          uschar seq_buff[4];                   /* accumulate utf-8 here */
 
          /* Manually track tainting, as we deal in individual chars below */
@@ -7888,6 +7902,15 @@ NOT_ITEM: ;
                if (--bytes_left == 0)          /* codepoint complete */
                  if(codepoint > 0x10FFFF)      /* is it too large? */
                    complete = -1;      /* error (RFC3629 limit) */
+                 else if ( (codepoint & 0x1FF800 ) == 0xD800 ) /* surrogate */
+                   /* A UTF-16 surrogate (which should be one of a pair that
+                   encode a Unicode codepoint that is outside the Basic
+                   Multilingual Plane).  Error, not UTF8.
+                   RFC2279.2 is slightly unclear on this, but 
+                   https://unicodebook.readthedocs.io/issues.html#strict-utf8-decoder
+                   says "Surrogates characters are also invalid in UTF-8:
+                   characters in U+D800—U+DFFF have to be rejected." */
+                   complete = -1;
                  else
                    {           /* finished; output utf-8 sequence */
                    yield = string_catn(yield, seq_buff, seq_len);
@@ -7897,27 +7920,25 @@ NOT_ITEM: ;
              }
            else        /* no bytes left: new sequence */
              {
-             if(!(c & 0x80))   /* 1-byte sequence, US-ASCII, keep it */
+             if (!(c & 0x80))  /* 1-byte sequence, US-ASCII, keep it */
                {
                yield = string_catn(yield, &c, 1);
                continue;
                }
-             if((c & 0xe0) == 0xc0)            /* 2-byte sequence */
-               {
-               if(c == 0xc0 || c == 0xc1)      /* 0xc0 and 0xc1 are illegal */
+             if ((c & 0xe0) == 0xc0)           /* 2-byte sequence */
+               if (c == 0xc0 || c == 0xc1)     /* 0xc0 and 0xc1 are illegal */
                  complete = -1;
                else
                  {
-                   bytes_left = 1;
-                   codepoint = c & 0x1f;
+                 bytes_left = 1;
+                 codepoint = c & 0x1f;
                  }
-               }
-             else if((c & 0xf0) == 0xe0)               /* 3-byte sequence */
+             else if ((c & 0xf0) == 0xe0)              /* 3-byte sequence */
                {
                bytes_left = 2;
                codepoint = c & 0x0f;
                }
-             else if((c & 0xf8) == 0xf0)               /* 4-byte sequence */
+             else if ((c & 0xf8) == 0xf0)              /* 4-byte sequence */
                {
                bytes_left = 3;
                codepoint = c & 0x07;
@@ -7991,7 +8012,7 @@ NOT_ITEM: ;
            goto EXPAND_FAILED;
            }
          yield = string_cat(yield, s);
-         DEBUG(D_expand) debug_printf_indent("yield: '%s'\n", string_from_gstring(yield));
+         DEBUG(D_expand) debug_printf_indent("yield: '%Y'\n", yield);
          break;
          }