From: Mark Slee Date: Thu, 31 Jan 2008 01:49:16 +0000 (+0000) Subject: Validate that throws clauses in Thrift contain only exceptions X-Git-Tag: 0.2.0~1022 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=91f2b7b2f71dc67bc90f5eed0de99dbb9317d793;p=common%2Fthrift.git Validate that throws clauses in Thrift contain only exceptions Summary: Throwing non-exceptions, though allowed in some languages, is a weird concept and causes problems in many places. Disallow it in the Thrift compiler and throw an informative error. Reviewed By: dreiss Test Plan: Generate a file with an int or something in a throws clause. Peep the nice informative compiler error. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665450 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/compiler/cpp/src/main.cc b/compiler/cpp/src/main.cc index e0eb4b71..9f3eb731 100644 --- a/compiler/cpp/src/main.cc +++ b/compiler/cpp/src/main.cc @@ -753,6 +753,20 @@ void validate_field_value(t_field* field, t_const_value* cv) { validate_const_rec(field->get_name(), field->get_type(), cv); } +/** + * Check that all the elements of a throws block are actually exceptions. + */ +bool validate_throws(t_struct* throws) { + const vector& members = throws->get_members(); + vector::const_iterator m_iter; + for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { + if (!(*m_iter)->get_type()->is_xception()) { + return false; + } + } + return true; +} + /** * Parses a program */ diff --git a/compiler/cpp/src/main.h b/compiler/cpp/src/main.h index f8828afe..3ab27444 100644 --- a/compiler/cpp/src/main.h +++ b/compiler/cpp/src/main.h @@ -49,6 +49,11 @@ void validate_const_type(t_const* c); */ void validate_field_value(t_field* field, t_const_value* cv); +/** + * Check members of a throws block + */ +bool validate_throws(t_struct* throws); + /** * Converts a string filename into a thrift program name */ diff --git a/compiler/cpp/src/thrifty.yy b/compiler/cpp/src/thrifty.yy index 2b099720..0b873866 100644 --- a/compiler/cpp/src/thrifty.yy +++ b/compiler/cpp/src/thrifty.yy @@ -766,6 +766,10 @@ Throws: { pdebug("Throws -> tok_throws ( FieldList )"); $$ = $3; + if (!validate_throws($$)) { + yyerror("Throws clause may not contain non-exception types"); + exit(1); + } } | {