Update pdfium_test filename generation to give helpful message

When pdfium_test needs to output filenames, those names have to be
restricted to a maximum length.  If this length is exceeded then it is
helpful to know the full string name which was too long.  This can't be
provided if the name is being built into a fixed-size array of the
maximum length.

Change the generation to instead use streaming to generate a string
which doesn't have a preset length limitation.  This allows for an
error message about the name to be provided in full.

Change-Id: I65ee1e8688b48c6af673f2ea2f9bbf7c32a0d344
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/105852
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Alan Screen <awscreen@chromium.org>
diff --git a/samples/pdfium_test_write_helper.cc b/samples/pdfium_test_write_helper.cc
index f951ad5..7ac49d7 100644
--- a/samples/pdfium_test_write_helper.cc
+++ b/samples/pdfium_test_write_helper.cc
@@ -6,6 +6,7 @@
 
 #include <limits.h>
 
+#include <sstream>
 #include <string>
 #include <utility>
 #include <vector>
@@ -182,32 +183,31 @@
 std::string GeneratePageOutputFilename(const char* pdf_name,
                                        int page_num,
                                        const char* extension) {
-  char filename[256];
-  int chars_formatted = snprintf(filename, sizeof(filename), "%s.%d.%s",
-                                 pdf_name, page_num, extension);
-  if (chars_formatted < 0 ||
-      static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
-    fprintf(stderr, "Filename %s is too long\n", filename);
+  std::ostringstream stream;
+  stream << pdf_name << "." << page_num << "." << extension;
+  std::string filename = stream.str();
+  if (filename.size() >= 256) {
+    fprintf(stderr, "Filename %s is too long\n", filename.c_str());
     return std::string();
   }
 
-  return std::string(filename);
+  return filename;
 }
 
 std::string GenerateImageOutputFilename(const char* pdf_name,
                                         int page_num,
                                         int image_num,
                                         const char* extension) {
-  char filename[256];
-  int chars_formatted = snprintf(filename, sizeof(filename), "%s.%d.%d.%s",
-                                 pdf_name, page_num, image_num, extension);
-  if (chars_formatted < 0 ||
-      static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
-    fprintf(stderr, "Filename %s for saving image is too long.\n", filename);
+  std::ostringstream stream;
+  stream << pdf_name << "." << page_num << "." << image_num << "." << extension;
+  std::string filename = stream.str();
+  if (filename.size() >= 256) {
+    fprintf(stderr, "Filename %s for saving image is too long.\n",
+            filename.c_str());
     return std::string();
   }
 
-  return std::string(filename);
+  return filename;
 }
 
 }  // namespace