public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: Qiang Ren <qiang.ren@microfocus.com>
To: "gcc-bugs@gcc.gnu.org" <gcc-bugs@gcc.gnu.org>
Subject: report a bug of gcc implementation of std::vsnprintf()
Date: Thu, 30 Mar 2023 08:29:45 +0000	[thread overview]
Message-ID: <DM6PR18MB27800EA091C72DDADC1354F9848E9@DM6PR18MB2780.namprd18.prod.outlook.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1311 bytes --]

Hi,

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++.

#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;
}

int main() {
  std::string s, s0, s1, s2;
  double d = 3.14;

 //the result is wrong
  Sprintf(s, "abc%.0f", d);
  std::cout << "s=" << s << "\n\n";

 //the result is wrong
  Sprintf(s0, "%.0f", d);
  std::cout << "s0=" << s0 << "\n\n";

  //the printf result is correct
  printf( "%.0f\n\n", d);

  //now it becomes correct
  Sprintf(s1, "%.0f", d);
  std::cout << "s1=" << s1 << "\n\n";

 //and this becomes correct too.
  Sprintf(s2, "abc%.0f", d);
  std::cout << "s2=" << s2 << "\n\n";
}


In a short word, printf() get the correct result, after calling it, vsnprintf() became correct too.

my build command:   g++ -o  t1  t1.cpp

Thanks,
Qiang Ren






             reply	other threads:[~2023-03-30  8:29 UTC|newest]

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

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=DM6PR18MB27800EA091C72DDADC1354F9848E9@DM6PR18MB2780.namprd18.prod.outlook.com \
    --to=qiang.ren@microfocus.com \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).