From c0092f973d79f309c1b89ae5e55ad8094b87763a Mon Sep 17 00:00:00 2001 From: David Reiss Date: Tue, 10 Jun 2008 22:55:46 +0000 Subject: [PATCH] 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. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@666365 13f79535-47bb-0310-9956-ffa450edef68 --- compiler/cpp/src/generate/t_php_generator.cc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/compiler/cpp/src/generate/t_php_generator.cc b/compiler/cpp/src/generate/t_php_generator.cc index 97502ffd..2a468f75 100644 --- a/compiler/cpp/src/generate/t_php_generator.cc +++ b/compiler/cpp/src/generate/t_php_generator.cc @@ -632,6 +632,22 @@ void t_php_generator::generate_php_struct_writer(ofstream& out, 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 << -- 2.17.1