summaryrefslogtreecommitdiffstats
path: root/ppapi/cpp
diff options
context:
space:
mode:
authorbbudge@google.com <bbudge@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-07 23:41:41 +0000
committerbbudge@google.com <bbudge@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2010-12-07 23:41:41 +0000
commita0feb4e193ecc822249a6ff8626a7cefa09d00d1 (patch)
tree95bc22811aea91693b96ff74c6febc13d3a44755 /ppapi/cpp
parent2caf8dc726b82626b934d9769205fc938d5c0a5f (diff)
downloadchromium_src-a0feb4e193ecc822249a6ff8626a7cefa09d00d1.zip
chromium_src-a0feb4e193ecc822249a6ff8626a7cefa09d00d1.tar.gz
chromium_src-a0feb4e193ecc822249a6ff8626a7cefa09d00d1.tar.bz2
Fix a bug in pp:Var (var.cc) where DebugString could throw an exception on Windows; replace _snprintf_s, which doesn't have the same signature as snprintf, with sprintf_s, which has the same function signature. Check length of string vars and truncate values (adding ellipsis at end) if the won't fit in our fixed size buffer.
TEST=manual BUG=none (found during testing in Chrome) Review URL: http://codereview.chromium.org/5559009 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@68540 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi/cpp')
-rw-r--r--ppapi/cpp/var.cc31
1 files changed, 20 insertions, 11 deletions
diff --git a/ppapi/cpp/var.cc b/ppapi/cpp/var.cc
index 5ade9b0..a1f6730 100644
--- a/ppapi/cpp/var.cc
+++ b/ppapi/cpp/var.cc
@@ -4,6 +4,7 @@
#include "ppapi/cpp/var.h"
+#include <stdio.h>
#include <string.h>
#include <algorithm>
@@ -16,10 +17,9 @@
#include "ppapi/cpp/module_impl.h"
#include "ppapi/cpp/dev/scriptable_object_deprecated.h"
-// Defining snprintf
-#include <stdio.h>
+// Define equivalent to snprintf on Windows.
#if defined(_MSC_VER)
-# define snprintf _snprintf_s
+# define snprintf sprintf_s
#endif
namespace {
@@ -343,23 +343,32 @@ Var Var::Call(const Var& method_name, const Var& arg1, const Var& arg2,
std::string Var::DebugString() const {
char buf[256];
- if (is_undefined())
+ if (is_undefined()) {
snprintf(buf, sizeof(buf), "Var<UNDEFINED>");
- else if (is_null())
+ } else if (is_null()) {
snprintf(buf, sizeof(buf), "Var<NULL>");
- else if (is_bool())
+ } else if (is_bool()) {
snprintf(buf, sizeof(buf), AsBool() ? "Var<true>" : "Var<false>");
- else if (is_int())
+ } else if (is_int()) {
// Note that the following static_cast is necessary because
// NativeClient's int32_t is actually "long".
// TODO(sehr,polina): remove this after newlib is changed.
snprintf(buf, sizeof(buf), "Var<%d>", static_cast<int>(AsInt()));
- else if (is_double())
+ } else if (is_double()) {
snprintf(buf, sizeof(buf), "Var<%f>", AsDouble());
- else if (is_string())
- snprintf(buf, sizeof(buf), "Var<'%s'>", AsString().c_str());
- else if (is_object())
+ } else if (is_string()) {
+ char format[] = "Var<'%s'>";
+ size_t decoration = sizeof(format) - 2; // The %s is removed.
+ size_t available = sizeof(buf) - decoration;
+ std::string str = AsString();
+ if (str.length() > available) {
+ str.resize(available - 3); // Reserve space for ellipsis.
+ str.append("...");
+ }
+ snprintf(buf, sizeof(buf), format, str.c_str());
+ } else if (is_object()) {
snprintf(buf, sizeof(buf), "Var<OBJECT>");
+ }
return buf;
}