Fossil SCM

Fix the non-standard %q format specifier in Fossil's custom printf() implementation so that the precision refers to the number of input characters, not the number of output characters.

drh 2025-07-09 13:01 trunk
Commit 0971536165b064ee6dc643c6782a2a20ef86938138e334e348de29be04861869
1 file changed +1 -1
+1 -1
--- src/printf.c
+++ src/printf.c
@@ -774,10 +774,11 @@
774774
char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote characters */
775775
char *escarg = va_arg(ap,char*);
776776
isnull = escarg==0;
777777
if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
778778
if( limit<0 ) limit = strlen(escarg);
779
+ if( precision>=0 && precision<limit ) limit = precision;
779780
for(i=n=0; i<limit; i++){
780781
if( escarg[i]==q ) n++;
781782
}
782783
needQuote = !isnull && xtype==etSQLESCAPE2;
783784
n += i + 1 + needQuote*2;
@@ -793,11 +794,10 @@
793794
if( ch==q ) bufpt[j++] = ch;
794795
}
795796
if( needQuote ) bufpt[j++] = q;
796797
bufpt[j] = 0;
797798
length = j;
798
- if( precision>=0 && precision<length ) length = precision;
799799
break;
800800
}
801801
case etHTMLIZE: {
802802
int limit = flag_alternateform ? va_arg(ap,int) : -1;
803803
char *zMem = va_arg(ap,char*);
804804
--- src/printf.c
+++ src/printf.c
@@ -774,10 +774,11 @@
774 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote characters */
775 char *escarg = va_arg(ap,char*);
776 isnull = escarg==0;
777 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
778 if( limit<0 ) limit = strlen(escarg);
 
779 for(i=n=0; i<limit; i++){
780 if( escarg[i]==q ) n++;
781 }
782 needQuote = !isnull && xtype==etSQLESCAPE2;
783 n += i + 1 + needQuote*2;
@@ -793,11 +794,10 @@
793 if( ch==q ) bufpt[j++] = ch;
794 }
795 if( needQuote ) bufpt[j++] = q;
796 bufpt[j] = 0;
797 length = j;
798 if( precision>=0 && precision<length ) length = precision;
799 break;
800 }
801 case etHTMLIZE: {
802 int limit = flag_alternateform ? va_arg(ap,int) : -1;
803 char *zMem = va_arg(ap,char*);
804
--- src/printf.c
+++ src/printf.c
@@ -774,10 +774,11 @@
774 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote characters */
775 char *escarg = va_arg(ap,char*);
776 isnull = escarg==0;
777 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
778 if( limit<0 ) limit = strlen(escarg);
779 if( precision>=0 && precision<limit ) limit = precision;
780 for(i=n=0; i<limit; i++){
781 if( escarg[i]==q ) n++;
782 }
783 needQuote = !isnull && xtype==etSQLESCAPE2;
784 n += i + 1 + needQuote*2;
@@ -793,11 +794,10 @@
794 if( ch==q ) bufpt[j++] = ch;
795 }
796 if( needQuote ) bufpt[j++] = q;
797 bufpt[j] = 0;
798 length = j;
 
799 break;
800 }
801 case etHTMLIZE: {
802 int limit = flag_alternateform ? va_arg(ap,int) : -1;
803 char *zMem = va_arg(ap,char*);
804

Keyboard Shortcuts

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