Fossil SCM

Fix the TH_ListAppend() function in TH1 so that it correctly escapes strings that have a '}' character that has no matching '{' to its left. Fix for ticket [4d73b4a2258a78e2].

drh 2022-11-12 12:32 trunk
Commit 4ec65ebba9177429ce8caafd49d2f2c1ee52679bca14cd703baaaa8253558590
1 file changed +13 -4
+13 -4
--- src/th.c
+++ src/th.c
@@ -1750,12 +1750,12 @@
17501750
int nElem /* Length of nElem */
17511751
){
17521752
Buffer output;
17531753
int i;
17541754
1755
- int hasSpecialChar = 0;
1756
- int hasEscapeChar = 0;
1755
+ int hasSpecialChar = 0; /* Whitespace or {}[]'" */
1756
+ int hasEscapeChar = 0; /* '}' without matching '{' to the left or a '\\' */
17571757
int nBrace = 0;
17581758
17591759
output.zBuf = *pzList;
17601760
output.nBuf = *pnList;
17611761
output.nBufAlloc = output.nBuf;
@@ -1768,13 +1768,22 @@
17681768
}
17691769
17701770
for(i=0; i<nElem; i++){
17711771
char c = zElem[i];
17721772
if( th_isspecial(c) ) hasSpecialChar = 1;
1773
- if( c=='\\' ) hasEscapeChar = 1;
1773
+ if( c=='\\' ){ hasEscapeChar = 1; break; }
17741774
if( c=='{' ) nBrace++;
1775
- if( c=='}' ) nBrace--;
1775
+ if( c=='}' ){
1776
+ if( nBrace==0 ){
1777
+ /* A closing brace that does not have a matching open brace to
1778
+ ** its left needs to be excaped. See ticket 4d73b4a2258a78e2 */
1779
+ hasEscapeChar = 1;
1780
+ break;
1781
+ }else{
1782
+ nBrace--;
1783
+ }
1784
+ }
17761785
}
17771786
17781787
if( nElem==0 || (!hasEscapeChar && hasSpecialChar && nBrace==0) ){
17791788
thBufferAddChar(interp, &output, '{');
17801789
thBufferWrite(interp, &output, zElem, nElem);
17811790
--- src/th.c
+++ src/th.c
@@ -1750,12 +1750,12 @@
1750 int nElem /* Length of nElem */
1751 ){
1752 Buffer output;
1753 int i;
1754
1755 int hasSpecialChar = 0;
1756 int hasEscapeChar = 0;
1757 int nBrace = 0;
1758
1759 output.zBuf = *pzList;
1760 output.nBuf = *pnList;
1761 output.nBufAlloc = output.nBuf;
@@ -1768,13 +1768,22 @@
1768 }
1769
1770 for(i=0; i<nElem; i++){
1771 char c = zElem[i];
1772 if( th_isspecial(c) ) hasSpecialChar = 1;
1773 if( c=='\\' ) hasEscapeChar = 1;
1774 if( c=='{' ) nBrace++;
1775 if( c=='}' ) nBrace--;
 
 
 
 
 
 
 
 
 
1776 }
1777
1778 if( nElem==0 || (!hasEscapeChar && hasSpecialChar && nBrace==0) ){
1779 thBufferAddChar(interp, &output, '{');
1780 thBufferWrite(interp, &output, zElem, nElem);
1781
--- src/th.c
+++ src/th.c
@@ -1750,12 +1750,12 @@
1750 int nElem /* Length of nElem */
1751 ){
1752 Buffer output;
1753 int i;
1754
1755 int hasSpecialChar = 0; /* Whitespace or {}[]'" */
1756 int hasEscapeChar = 0; /* '}' without matching '{' to the left or a '\\' */
1757 int nBrace = 0;
1758
1759 output.zBuf = *pzList;
1760 output.nBuf = *pnList;
1761 output.nBufAlloc = output.nBuf;
@@ -1768,13 +1768,22 @@
1768 }
1769
1770 for(i=0; i<nElem; i++){
1771 char c = zElem[i];
1772 if( th_isspecial(c) ) hasSpecialChar = 1;
1773 if( c=='\\' ){ hasEscapeChar = 1; break; }
1774 if( c=='{' ) nBrace++;
1775 if( c=='}' ){
1776 if( nBrace==0 ){
1777 /* A closing brace that does not have a matching open brace to
1778 ** its left needs to be excaped. See ticket 4d73b4a2258a78e2 */
1779 hasEscapeChar = 1;
1780 break;
1781 }else{
1782 nBrace--;
1783 }
1784 }
1785 }
1786
1787 if( nElem==0 || (!hasEscapeChar && hasSpecialChar && nBrace==0) ){
1788 thBufferAddChar(interp, &output, '{');
1789 thBufferWrite(interp, &output, zElem, nElem);
1790

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button