public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* report a bug of gcc implementation of std::vsnprintf()
@ 2023-03-30  8:29 Qiang Ren
  2023-03-30  9:42 ` Jakub Jelinek
  0 siblings, 1 reply; 2+ messages in thread
From: Qiang Ren @ 2023-03-30  8:29 UTC (permalink / raw)
  To: gcc-bugs

[-- 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






^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: report a bug of gcc implementation of std::vsnprintf()
  2023-03-30  8:29 report a bug of gcc implementation of std::vsnprintf() Qiang Ren
@ 2023-03-30  9:42 ` Jakub Jelinek
  0 siblings, 0 replies; 2+ messages in thread
From: Jakub Jelinek @ 2023-03-30  9:42 UTC (permalink / raw)
  To: Qiang Ren; +Cc: gcc-bugs

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-03-30  9:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-30  8:29 report a bug of gcc implementation of std::vsnprintf() Qiang Ren
2023-03-30  9:42 ` Jakub Jelinek

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