libs/libc/obstack: correctly append null byte at the end of string

obstack_printf and obstack_vprintf should terminate the C string with
null byte but lib_vsprintf doesn't do it. It must be done on top of
that unless we get unterminated string.

This is fix to be consistent with GlibC behavior.

This also includes minor tweak to use obstack_1grow directly instead of
calling obstack_puts.
This commit is contained in:
Karel Kočí 2024-08-21 13:10:55 +02:00 committed by Alan Carvalho de Assis
parent ee6fdb2c85
commit c4d8d937d5

View file

@ -54,8 +54,11 @@ static int obstack_puts(FAR struct lib_outstream_s *self,
static void obstack_putc(FAR struct lib_outstream_s *self, int ch) static void obstack_putc(FAR struct lib_outstream_s *self, int ch)
{ {
char tmp = ch; FAR struct obstack_stream *stream = (FAR struct obstack_stream *)self;
obstack_puts(self, &tmp, 1);
DEBUGASSERT(self);
obstack_1grow(stream->h, ch);
} }
/**************************************************************************** /****************************************************************************
@ -93,5 +96,14 @@ int obstack_vprintf(FAR struct obstack *h, FAR const char *fmt, va_list ap)
outstream.common.nput = 0; outstream.common.nput = 0;
outstream.h = h; outstream.h = h;
return lib_vsprintf(&outstream.common, fmt, ap); int nbytes = lib_vsprintf(&outstream.common, fmt, ap);
if (nbytes < 0)
{
obstack_free(h, obstack_finish(h));
return ERROR;
}
obstack_1grow(h, '\0');
return nbytes;
} }