pelegm / google-glog

Automatically exported from code.google.com/p/google-glog
Other
0 stars 0 forks source link

misunderstanding of the return value of snprintf #182

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. save the following lines as "test.cpp"
#include <glog/logging.h>
#include <glog/raw_logging.h>
#include <string>
int main(){
  std::string s(2955,'a');
  RAW_LOG(ERROR,s.c_str());
  RAW_LOG(ERROR,s.c_str());
  return 0;
}
2.compile it
# g++ -g3 -o t test.cpp -lglog 
3. run it
./t

What is the expected output? What do you see instead?
2 lines output on the screen
E0100 00:00:00.000000 25136 test.cpp:7] RAW: aaaaaaaaaaaa...
E0100 00:00:00.000000 25136 test.cpp:7] RAW: aaaaaaaaaaaa...

But instead of it, you may got only one line.
E0100 00:00:00.000000 25136 test.cpp:6] RAW: aaaaa....E0100 00:00:00.000000 
25136 test.cpp:7] RAW: ....

The two lines were joined together.

What version of the product are you using? On what operating system?
glog-0.3.3
Fedora 20
Linux cmlinux 3.12.6-300.fc20.x86_64 #1 SMP Mon Dec 23 16:44:31 UTC 2013 x86_64 
x86_64 x86_64 GNU/Linux

Please provide any additional information below.
raw_logging.cc:
// Helper for RawLog__ below.
inline static bool VADoRawLog(char** buf, int* size,
                              const char* format, va_list ap) {
  int n = vsnprintf(*buf, *size, format, ap);
  if (n < 0 || n > *size) return false;
  *size -= n;
  *buf += n;
  return true;
}

int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...)
When Glibc's vsnprintf and snprintf function truncated output, it returns a 
value >= size ,or -1. 
So you should change n>*size to n>=*size to test if it was successful.

Original issue reported on code.google.com by me@sunchangming.com on 14 Jan 2014 at 11:07