mirror of https://github.com/ipxe/ipxe.git
Correctly handle zero-length buffers.
parent
25f5d114a0
commit
efd6281a35
|
@ -171,7 +171,7 @@ static char * format_decimal ( char *end, signed long num, int width ) {
|
||||||
* @v args Arguments corresponding to the format string
|
* @v args Arguments corresponding to the format string
|
||||||
* @ret len Length of formatted string
|
* @ret len Length of formatted string
|
||||||
*/
|
*/
|
||||||
int vcprintf ( struct printf_context *ctx, const char *fmt, va_list args ) {
|
size_t vcprintf ( struct printf_context *ctx, const char *fmt, va_list args ) {
|
||||||
int flags;
|
int flags;
|
||||||
int width;
|
int width;
|
||||||
uint8_t *length;
|
uint8_t *length;
|
||||||
|
@ -296,14 +296,8 @@ static void printf_sputc ( struct printf_context *ctx, unsigned int c ) {
|
||||||
*/
|
*/
|
||||||
int vsnprintf ( char *buf, size_t size, const char *fmt, va_list args ) {
|
int vsnprintf ( char *buf, size_t size, const char *fmt, va_list args ) {
|
||||||
struct printf_context ctx;
|
struct printf_context ctx;
|
||||||
int len;
|
size_t len;
|
||||||
|
size_t end;
|
||||||
/* Ensure last byte is NUL if a size is specified. This
|
|
||||||
* catches the case of the buffer being too small, in which
|
|
||||||
* case a trailing NUL would not otherwise be added.
|
|
||||||
*/
|
|
||||||
if ( size != PRINTF_NO_LENGTH )
|
|
||||||
buf[size-1] = '\0';
|
|
||||||
|
|
||||||
/* Hand off to vcprintf */
|
/* Hand off to vcprintf */
|
||||||
ctx.handler = printf_sputc;
|
ctx.handler = printf_sputc;
|
||||||
|
@ -312,7 +306,12 @@ int vsnprintf ( char *buf, size_t size, const char *fmt, va_list args ) {
|
||||||
len = vcprintf ( &ctx, fmt, args );
|
len = vcprintf ( &ctx, fmt, args );
|
||||||
|
|
||||||
/* Add trailing NUL */
|
/* Add trailing NUL */
|
||||||
printf_sputc ( &ctx, '\0' );
|
if ( size ) {
|
||||||
|
end = size - 1;
|
||||||
|
if ( len < end )
|
||||||
|
end = len;
|
||||||
|
buf[end] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue