Opened 9 years ago

Closed 5 years ago

#8735 closed Bugs (invalid)

boost::format crashes on "%0%" format string

Reported by: oliora@… Owned by: James E. King, III
Milestone: To Be Determined Component: format
Version: Boost 1.51.0 Severity: Problem
Keywords: format Cc: oliora@…

Description

Application crashes at runtime at the following code

boost::format("%0%") % "something";

I understand that 0 is a wrong format argument number (they are starting from 1), but it's too easy to forget about it (for example, Python uses 0 based format argument numbers). While boost::format is considered to be a safe alternative to printf, I think such mistakes should be handled and at least don't give a runtime crash.

Change History (2)

comment:1 by James E. King, III, 5 years ago

Owner: changed from Samuel Krempp to James E. King, III
Status: newassigned
Summary: boost::format craches on "%0%" format stringboost::format crashes on "%0%" format string

comment:2 by James E. King, III, 5 years ago

Resolution: invalid
Status: assignedclosed

There is no crash in this case, but an unhandled exception which is the responsibility of the consuming application. I will add the following unit test anyway, but the report is invalid.

jking@ubuntu:~/boost/libs/format/test$ git diff HEAD
diff --git a/test/format_test3.cpp b/test/format_test3.cpp
index 231a91b..6cc9a65 100644
--- a/test/format_test3.cpp
+++ b/test/format_test3.cpp
@@ -128,5 +128,8 @@ int test_main(int, char* [])
     std::string arg; // empty string  
     s = str(format("%=8s") % arg);
 
+    // https://svn.boost.org/trac10/ticket/8735
+    BOOST_CHECK_THROW(s = str(format("%0%") % 1234567), boost::io::bad_format_string);
+
     return 0;
 }
Note: See TracTickets for help on using tickets.