fixing wrong termination of string
Subsystems (2)
PATCH 1/1 fixing wrong termination of string
Hi,
I discovered a small bug in lib/vsprintf.c which leads to an "Access violation(2)" when I tried to tftp a file, in QEMU. If CONFIG_SYS_VSNPRINTF is set, the str pointer is incremented even if str reached the end of string (str == end) because of ADDCH. This leads to a wrong length of string and causes the problem.
Here is the patch:
lib/vsprintf.c +3 -2
@@ -747,8 +747,9 @@ repeat:#ifdef CONFIG_SYS_VSNPRINTF if (size >0 ) { -ADDCH (str,'\0' ); -if (str > end) +if (str < end) + *str ='\0' ; +else end[-1 ] ='\0' ; }
#else
Regards
Daniel
HEIG-VD
Haute Ecole d'Ing?nierie et de Gestion du Canton de Vaud School of Business and Engineering Vaud
Dr Daniel Rossier
Professeur HES
D?partement TIC
Institut REDS (Reconfigurable Embedded Digital Systems) T?l. :
0041 24 55 762 69
Mobile :
0041 79 29 254 58
daniel.rossier at heig-vd.ch<mailto:[email protected]> Route de Cheseaux 1
CH-1401 Yverdon-les-Bains
http://www.heig-vd.ch<http://www.heig-vd.ch/>
[Annonce HEIG-VD]<http://go.heig-vd.ch/bannerEmail/>
PATCH 1/1 fixing wrong termination of string
Quoted 14 lines · Rossier Daniel
Hi,
I discovered a small bug in
lib/vsprintf.cwhich leads to an "Access violation(2)" when I tried to tftp a file, in QEMU.
IfCONFIG_SYS_VSNPRINTFis set, the str pointer is incremented even if str reached the end of string (str == end) because of ADDCH.
This leads to a wrong length of string and causes the problem. Here is the patch:lib/vsprintf.c +0
-0
lib/vsprintf.c repeat: · line 753
Quoted 9 lines
lib/vsprintf.c +3
-2@@ -747,8 +747,9 @@ repeat:#ifdef CONFIG_SYS_VSNPRINTF if (size >0 ) { -ADDCH (str,'\0' ); -if (str > end) +if (str < end) + *str ='\0' ; +else end[-1 ] ='\0' ;
This is good - but can I suggest going a little further, maybe:
+ if (str < end)
lib/vsprintf.c repeat: · line 752
Quoted 4 lines
lib/vsprintf.c +2
-0+ *str ='\0' ; + else if (end > buf) end[-1 ] ='\0' ;
since I think it is actually valid to call this function with a size of 0, perhaps to find out the length that would be produced.
Regards,
Simon