public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Qiang Ren <qiang.ren@microfocus.com>
Cc: "gcc-bugs@gcc.gnu.org" <gcc-bugs@gcc.gnu.org>
Subject: Re: report a bug of gcc implementation of std::vsnprintf()
Date: Thu, 30 Mar 2023 11:42:35 +0200	[thread overview]
Message-ID: <ZCVZi9aJpbjYf0+C@tucnak> (raw)
In-Reply-To: <DM6PR18MB27800EA091C72DDADC1354F9848E9@DM6PR18MB2780.namprd18.prod.outlook.com>

On Thu, Mar 30, 2023 at 08:29:45AM +0000, Qiang Ren via Gcc-bugs wrote:
> I found a bug in the result of std::vsnprintf(), here is the test app that can reproduce it.
> I tested with g++ 11.2 and 12.2 and both have the bug,  and this issue does not happen with visual c++.

gcc-bugs is not the right way to report GCC bugs, though in this case
the only bug is in your program.
You can't use the same va_list multiple times as you pass it to
std::vsnprintf, either you need to use va_copy before the first call
and use the copy in one of the two calls, or you need to va-end after
the first vsnprintf call and va_start again before the second call.
> 
> #include <stdio.h>
> #include <string>
> #include <iostream>
> #include <cstdarg>
> 
> int Sprintf(std::string& s, const char* pszFormat, ...) {
>   va_list argptr;
>   va_start(argptr, pszFormat);
> 
>   auto n = std::vsnprintf(nullptr, 0, pszFormat, argptr);
>   char* buf = (char*)malloc(n + 1);
>   n = std::vsnprintf(buf, n + 1, pszFormat, argptr);
>   s = buf;
>   free(buf);
> 
>   va_end(argptr);
>   return n;
> }

	Jakub


      reply	other threads:[~2023-03-30  9:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-30  8:29 Qiang Ren
2023-03-30  9:42 ` Jakub Jelinek [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZCVZi9aJpbjYf0+C@tucnak \
    --to=jakub@redhat.com \
    --cc=gcc-bugs@gcc.gnu.org \
    --cc=qiang.ren@microfocus.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).