You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by dr...@apache.org on 2008/06/11 00:55:46 UTC

svn commit: r666365 - /incubator/thrift/trunk/compiler/cpp/src/generate/t_php_generator.cc

Author: dreiss
Date: Tue Jun 10 15:55:46 2008
New Revision: 666365

URL: http://svn.apache.org/viewvc?rev=666365&view=rev
Log:
PHP Generator: Throw an exception on bad types.

Summary:
Previously, if you set a structure field to, say, an int when it was
supposed to be a container (array) or struct (object), the serialization
code would either produce corrupt output (container) or cause a fatal
error (struct).  Now they both throw exceptions.

Reviewed By: mcslee

Test Plan: Looked at generated code.

Modified:
    incubator/thrift/trunk/compiler/cpp/src/generate/t_php_generator.cc

Modified: incubator/thrift/trunk/compiler/cpp/src/generate/t_php_generator.cc
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/compiler/cpp/src/generate/t_php_generator.cc?rev=666365&r1=666364&r2=666365&view=diff
==============================================================================
--- incubator/thrift/trunk/compiler/cpp/src/generate/t_php_generator.cc (original)
+++ incubator/thrift/trunk/compiler/cpp/src/generate/t_php_generator.cc Tue Jun 10 15:55:46 2008
@@ -632,6 +632,22 @@
       indent() << "if ($this->" << (*f_iter)->get_name() << " !== null) {" << endl;
     indent_up();
 
+    t_type* type = get_true_type((*f_iter)->get_type());
+    string expect;
+    if (type->is_container()) {
+      expect = "array";
+    } else if (type->is_struct()) {
+      expect = "object";
+    }
+    if (!expect.empty()) {
+      out <<
+        indent() << "if (!is_" << expect << "($this->" << (*f_iter)->get_name() << ")) {" << endl;
+      indent_up();
+      out <<
+        indent() << "throw new TProtocolException('Bad type in structure.', TProtocolException::INVALID_DATA);" << endl;
+      scope_down(out);
+    }
+
     // Write field header
     if (binary_inline_) {
       out <<