nuttx/libs/libc/obstack/lib_obstack_vprintf.c
Karel Kočí f2fd0bc148 libs/libc/obstack: revert invalid null byte append to obstack_vprintf
The commit c4d8d937d5 added null byte for
obstack_vprintf and thus to obstack_printf. I probably lost my marbles
but the same test I run previously now won't append null byte at the end
and this "fix" now causes regressions in code using obstack.

For the reference this is the testing code:

  #define _GNU_SOURCE
  #include <obstack.h>
  #include <stdarg.h>
  #include <stdio.h>
  #include <stdlib.h>

  #define obstack_chunk_alloc malloc
  #define obstack_chunk_free free

  static void print(struct obstack *obs, const char *fmt, ...) {
    va_list arg;
    va_start(arg, fmt);
    obstack_vprintf(obs, fmt, arg);
    va_end(arg);
  }

  int main(int argc, char *argv[]) {
    struct obstack obs;
    obstack_init(&obs);

    obstack_printf(&obs, "this %s", "text ");
    obstack_printf(&obs, "is appended");
    print(&obs, " as well as %s", "vprintf text");
    obstack_1grow(&obs, '\0');

    printf("%s\n", (char *)obstack_finish(&obs));

    obstack_free(&obs, NULL);
    return 0;
  }

The output on NuttX without this patch:

  > obstest
  this text

The output on NuttX with this patch:

  > obstest
  this text is appended as well as vprintf text

The output with GlibC:

  $ gcc test.c && ./a.out
  this text is appended as well as vprintf text
  $ ldd a.out
  	linux-vdso.so.1 (0x00007ffff7fc5000)
  	libc.so.6 => /nix/store/3dyw8dzj9ab4m8hv5dpyx7zii8d0w6fi-glibc-2.39-52/lib/libc.so.6 (0x00007ffff7dc8000)
  	/nix/store/3dyw8dzj9ab4m8hv5dpyx7zii8d0w6fi-glibc-2.39-52/lib/ld-linux-x86-64.so.2 => /nix/store/3dyw8dzj9ab4m8hv5dpyx7zii8d0w6fi-glibc-2.39-52/lib64/ld-linux-x86-64.so.2 (0x00007ffff7fc7000)

The output with Musl (and obstack_vprintf removed):

  $ gcc -lobstack test-musl.c && ./a.out
  this text is appended
  $ ldd a.out
  	/nix/store/00w9nz0343pxk7hbsjzq9bzaby65hk4g-musl-1.2.3/lib/ld-musl-x86_64.so.1 (0x7ffff7f4b000)
  	libobstack.so.1 => /nix/store/qvv16dqn85qwz9vz9wvpnv435z0n5msr-musl-obstack-1.2.3/lib/libobstack.so.1 (0x7ffff7f3b000)
  	libc.so => /nix/store/00w9nz0343pxk7hbsjzq9bzaby65hk4g-musl-1.2.3/lib/ld-musl-x86_64.so.1 (0x7ffff7f4b000)
2024-09-17 11:32:48 +08:00

109 lines
3.4 KiB
C

/****************************************************************************
* libs/libc/obstack/lib_obstack_vprintf.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <obstack.h>
#include <assert.h>
#include <nuttx/streams.h>
/****************************************************************************
* Private Types
****************************************************************************/
struct obstack_stream
{
struct lib_outstream_s common;
FAR struct obstack *h;
};
/****************************************************************************
* Private Functions
****************************************************************************/
static int obstack_puts(FAR struct lib_outstream_s *self,
FAR const void *buf, int len)
{
FAR struct obstack_stream *stream = (FAR struct obstack_stream *)self;
DEBUGASSERT(self);
obstack_grow(stream->h, buf, len);
return len;
}
static void obstack_putc(FAR struct lib_outstream_s *self, int ch)
{
FAR struct obstack_stream *stream = (FAR struct obstack_stream *)self;
DEBUGASSERT(self);
obstack_1grow(stream->h, ch);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: obstack_vprintf
*
* Description:
* This is similar to the vasprintf except it uses obstack to allocate
* string on. The characters are written onto the end of the currently
* growing object and terminated by null byte.
*
* The same remarks are applied here as for obstack_printf regarding the
* definition location in GlibC.
*
* Input Parameters:
* h: pointer to the handle used to grow the object.
* fmt: format string
* ap: format string input as a variable argument list
*
* Returned Value:
* Number of characters added to the obstack excluding the null byte.
*
****************************************************************************/
int obstack_vprintf(FAR struct obstack *h, FAR const char *fmt, va_list ap)
{
struct obstack_stream outstream;
outstream.common.putc = obstack_putc;
outstream.common.puts = obstack_puts;
outstream.common.flush = lib_noflush;
outstream.common.nput = 0;
outstream.h = h;
int nbytes = lib_vsprintf(&outstream.common, fmt, ap);
if (nbytes < 0)
{
obstack_free(h, obstack_finish(h));
return ERROR;
}
return nbytes;
}