diff options
author | Michi Henning <michi@zeroc.com> | 2003-05-23 08:07:40 +0000 |
---|---|---|
committer | Michi Henning <michi@zeroc.com> | 2003-05-23 08:07:40 +0000 |
commit | 5c89803b442f059bd7444b286adb3e0a1c4afeca (patch) | |
tree | e79bae21964782dc9a89df7ae2d0985caa822c26 /cpp/src | |
parent | VC70 install improvements (diff) | |
download | ice-5c89803b442f059bd7444b286adb3e0a1c4afeca.tar.bz2 ice-5c89803b442f059bd7444b286adb3e0a1c4afeca.tar.xz ice-5c89803b442f059bd7444b286adb3e0a1c4afeca.zip |
Fixed the bug that Benoit found yesterday in both ice and icej.
(Zero-length sequence containing class elements caused an crash.)
:Ice::Object). Obviously, this would cause an assertion failure or a
{{{ClassCastException}}}. The solution is simply to force the down-cast
in the generated patch function and to check whether the cast failed:
if so, the class was sliced too much and we throw a
{{{NoObjectFactoryException}}} as we should. (For Java, the exception
is thrown from {{{BasicStream.readObject}}}, after catching a
{{{ClassCastException}}}. For C++, the exception is thrown directly
from the generated patch function.)
:Ice::Object}}} and {{{::Printer}}}.
Updated the code generator to correctly initialize the {{{type}}} member of
a {{{NoObjectFactoryException}}} with the type ID of the type that
wasn't found. (Unfortunately, this involved modifying {{{Parser.h}}},
so you will have to rebuild both ice and icej.)
Added code generation for custom sequence types for icej. Generated code
compiles and looks OK, but I haven't written tests yet, so there may
still be a latent bug in this.
Diffstat (limited to 'cpp/src')
-rw-r--r-- | cpp/src/Slice/JavaUtil.cpp | 21 | ||||
-rw-r--r-- | cpp/src/Slice/Parser.cpp | 76 | ||||
-rw-r--r-- | cpp/src/slice2cpp/Gen.cpp | 7 | ||||
-rw-r--r-- | cpp/src/slice2java/Gen.cpp | 180 |
4 files changed, 205 insertions, 79 deletions
diff --git a/cpp/src/Slice/JavaUtil.cpp b/cpp/src/Slice/JavaUtil.cpp index 8986ff98b3c..a5b55146e1f 100644 --- a/cpp/src/Slice/JavaUtil.cpp +++ b/cpp/src/Slice/JavaUtil.cpp @@ -990,10 +990,25 @@ Slice::JavaGenerator::writeSequenceMarshalUnmarshalCode(Output& out, out << nl << "for(int __i" << iter << " = 0; __i" << iter << " < __len" << iter << "; __i" << iter << "++)"; out << sb; - out << nl << origContentS << " __elem;"; + BuiltinPtr builtin = BuiltinPtr::dynamicCast(origContent); + if((builtin && builtin->kind() == Builtin::KindObject) || ClassDeclPtr::dynamicCast(origContent)) + { + out << nl << v << ".add(null);"; + ostringstream patchParams; + patchParams << v << ", __i" << iter; + writeMarshalUnmarshalCode(out, scope, seq->type(), "__elem", false, iter, false, + list<string>(), patchParams.str()); + } + else + { + out << nl << origContentS << " __elem;"; + writeMarshalUnmarshalCode(out, scope, seq->type(), "__elem", false, iter, false); + } iter++; - writeMarshalUnmarshalCode(out, scope, seq->type(), "__elem", false, iter, false); - out << nl << v << ".add(__elem);"; + if((builtin && builtin->kind() != Builtin::KindObject) && !ClassDeclPtr::dynamicCast(origContent)) + { + out << nl << v << ".add(__elem);"; + } out << eb; } } diff --git a/cpp/src/Slice/Parser.cpp b/cpp/src/Slice/Parser.cpp index 869b9ba115f..f04292a71d5 100644 --- a/cpp/src/Slice/Parser.cpp +++ b/cpp/src/Slice/Parser.cpp @@ -84,6 +84,70 @@ Slice::Builtin::isLocal() const return _kind == KindLocalObject; } +string +Slice::Builtin::typeId() const +{ + switch(_kind) + { + case KindByte: + { + return "::Ice::Byte"; + break; + } + case KindBool: + { + return "::Ice::Bool"; + break; + } + case KindShort: + { + return "::Ice::Short"; + break; + } + case KindInt: + { + return "::Ice::Int"; + break; + } + case KindLong: + { + return "::Ice::Long"; + break; + } + case KindFloat: + { + return "::Ice::Float"; + break; + } + case KindDouble: + { + return "::Ice::Double"; + break; + } + case KindString: + { + return "::Ice::String"; + break; + } + case KindObject: + { + return "::Ice::Object"; + break; + } + case KindObjectProxy: + { + return "::Ice::Object*"; + break; + } + case KindLocalObject: + { + return "::Ice::LocalObject"; + break; + } + } + assert(false); +} + bool Slice::Builtin::usesClasses() const { @@ -1612,6 +1676,12 @@ Slice::Constructed::isLocal() const return _local; } +string +Slice::Constructed::typeId() const +{ + return scoped(); +} + ConstructedList Slice::Constructed::dependencies() { @@ -2395,6 +2465,12 @@ Slice::Proxy::isLocal() const return __class->isLocal(); } +string +Slice::Proxy::typeId() const +{ + return __class->scoped(); +} + bool Slice::Proxy::usesClasses() const { diff --git a/cpp/src/slice2cpp/Gen.cpp b/cpp/src/slice2cpp/Gen.cpp index 6a5de35ff21..97aebb857a4 100644 --- a/cpp/src/slice2cpp/Gen.cpp +++ b/cpp/src/slice2cpp/Gen.cpp @@ -2446,7 +2446,12 @@ Slice::Gen::ObjectVisitor::visitClassDefEnd(const ClassDefPtr& p) C << nl << scope << name << "Ptr* p = static_cast< " << scope << name << "Ptr*>(__addr);"; C << nl << "assert(p);"; C << nl << "*p = " << scope << name << "Ptr::dynamicCast(v);"; - C << nl << "assert(!v || *p);"; + C << nl << "if(v && !*p)"; + C << sb; + C << nl << "::Ice::NoObjectFactoryException e(__FILE__, __LINE__);"; + C << nl << "e.type = " << scope << name << "::ice_staticId();"; + C << nl << "throw e;"; + C << eb; C << eb; C << sp; diff --git a/cpp/src/slice2java/Gen.cpp b/cpp/src/slice2java/Gen.cpp index 88295a2bd1e..671e852fa03 100644 --- a/cpp/src/slice2java/Gen.cpp +++ b/cpp/src/slice2java/Gen.cpp @@ -1227,59 +1227,6 @@ Slice::Gen::TypesVisitor::visitClassDefStart(const ClassDefPtr& p) out << sb; -#if 0 - // - // hashCode - // - if(!p->isInterface()) - { - // - // Does the class have (or inherit) any data members? - // - bool baseHasDataMembers = false; - ClassList l = p->bases(); - while(!l.empty() && !l.front()->isInterface()) - { - if(l.front()->hasDataMembers()) - { - baseHasDataMembers = true; - break; - } - l = l.front()->bases(); - } - - if(p->hasDataMembers() || baseHasDataMembers) - { - out << sp << nl << "public int" << nl << "hashCode()"; - out << sb; - if(p->hasDataMembers()) - { - DataMemberList members = p->dataMembers(); - DataMemberList::const_iterator d; - - out << nl << "int __h = 0;"; - int iter = 0; - for(d = members.begin(); d != members.end(); ++d) - { - string memberName = fixKwd((*d)->name()); - list<string> metaData = (*d)->getMetaData(); - writeHashCode(out, (*d)->type(), memberName, iter, metaData); - } - if(baseHasDataMembers) - { - out << nl << "__h = 5 * __h + super.hashCode();"; - } - out << nl << "return __h;"; - } - else - { - out << nl << "return super.hashCode();"; - } - out << eb; - } - } -#endif - // // Default factory for non-abstract classes. // @@ -1359,6 +1306,10 @@ Slice::Gen::TypesVisitor::visitClassDefStart(const ClassDefPtr& p) out << nl << "case " << memberCount << ":"; out.inc(); } + if(allClassMembers.size() > 1) + { + out << nl << "__typeId = \"" << (*d)->type()->typeId() << "\";"; + } string memberName = fixKwd((*d)->name()); string memberScope = fixKwd((*d)->scope()); string memberType = typeToString((*d)->type(), TypeModeMember, memberScope); @@ -1374,9 +1325,23 @@ Slice::Gen::TypesVisitor::visitClassDefStart(const ClassDefPtr& p) out << eb; } out << eb; + + out << sp << nl << "public String" << nl << "type()"; + out << sb; if(allClassMembers.size() > 1) { - out << nl << nl << "private int __member;"; + out << nl << "return __typeId;"; + } + else + { + out << nl << "return \"" << (*allClassMembers.begin())->type()->typeId() << "\";"; + } + out << eb; + + if(allClassMembers.size() > 1) + { + out << sp << nl << "private int __member;"; + out << nl << "private String __typeId;"; } out << eb; } @@ -1561,6 +1526,10 @@ Slice::Gen::TypesVisitor::visitExceptionEnd(const ExceptionPtr& p) out << nl << "case " << memberCount << ":"; out.inc(); } + if(allClassMembers.size() > 1) + { + out << nl << "__typeId = \"" << (*d)->type()->typeId() << "\";"; + } string memberName = fixKwd((*d)->name()); string memberScope = fixKwd((*d)->scope()); string memberType = typeToString((*d)->type(), TypeModeMember, memberScope); @@ -1576,9 +1545,22 @@ Slice::Gen::TypesVisitor::visitExceptionEnd(const ExceptionPtr& p) out << eb; } out << eb; + + out << sp << nl << "public String" << nl << "type()"; + out << sb; + if(allClassMembers.size() > 1) + { + out << nl << "return __typeId;"; + } + else + { + out << nl << "return \"" << (*allClassMembers.begin())->type()->typeId() << "\";"; + } + out << eb; if(allClassMembers.size() > 1) { - out << nl << nl << "private int __member;"; + out << sp << nl << "private int __member;"; + out << nl << "private String __typeId;"; } out << eb; } @@ -1862,6 +1844,10 @@ Slice::Gen::TypesVisitor::visitStructEnd(const StructPtr& p) out << nl << "case " << memberCount << ":"; out.inc(); } + if(classMembers.size() > 1) + { + out << nl << "__typeId = \"" << (*d)->type()->typeId() << "\";"; + } string memberName = fixKwd((*d)->name()); string memberScope = fixKwd((*d)->scope()); string memberType = typeToString((*d)->type(), TypeModeMember, memberScope); @@ -1877,9 +1863,23 @@ Slice::Gen::TypesVisitor::visitStructEnd(const StructPtr& p) out << eb; } out << eb; + + out << sp << nl << "public String" << nl << "type()"; + out << sb; + if(classMembers.size() > 1) + { + out << nl << "return __typeId;"; + } + else + { + out << nl << "return \"" << (*classMembers.begin())->type()->typeId() << "\";"; + } + out << eb; + if(classMembers.size() > 1) { - out << nl << nl << "private int __member;"; + out << sp << nl << "private int __member;"; + out << nl << "private String __typeId;"; } out << eb; } @@ -2265,23 +2265,31 @@ Slice::Gen::HolderVisitor::writeHolder(const TypePtr& p) out << sb; out << nl << "this.value = value;"; out << eb; - BuiltinPtr builtin = BuiltinPtr::dynamicCast(p); - if((builtin && builtin->kind() == Builtin::KindObject) || ClassDeclPtr::dynamicCast(p)) + if(!p->isLocal()) { - out << sp << nl << "public class Patcher implements IceInternal.Patcher"; - out << sb; - out << nl << "public void"; - out << nl << "patch(Ice.Object v)"; - out << sb; - out << nl << "value = (" << typeS << ")v;"; - out << eb; - out << eb; + BuiltinPtr builtin = BuiltinPtr::dynamicCast(p); + if((builtin && builtin->kind() == Builtin::KindObject) || ClassDeclPtr::dynamicCast(p)) + { + out << sp << nl << "public class Patcher implements IceInternal.Patcher"; + out << sb; + out << nl << "public void"; + out << nl << "patch(Ice.Object v)"; + out << sb; + out << nl << "value = (" << typeS << ")v;"; + out << eb; - out << sp << nl << "public Patcher"; - out << nl << "getPatcher()"; - out << sb; - out << nl << "return new Patcher();"; - out << eb; + out << sp << nl << "public String" << nl << "type()"; + out << sb; + out << nl << "return \"" << p->typeId() << "\";"; + out << eb; + out << eb; + + out << sp << nl << "public Patcher"; + out << nl << "getPatcher()"; + out << sb; + out << nl << "return new Patcher();"; + out << eb; + } } out << sp << nl << "public " << typeS << " value;"; out << eb; @@ -2611,9 +2619,12 @@ Slice::Gen::HelperVisitor::visitSequence(const SequencePtr& p) // // The sequence has class elements. // + string metaData = findMetaData(p->getMetaData()); + string listType = metaData.empty() ? typeS : "java.util.List"; + out << sp << nl << "private static class Patcher implements IceInternal.Patcher"; out << sb; - out << sp << nl << "Patcher(" << typeS << " values, int index)"; + out << sp << nl << "Patcher(" << listType << " values, int index)"; out << sb; out << nl << "__values = values;"; out << nl << "__index = index;"; @@ -2621,10 +2632,23 @@ Slice::Gen::HelperVisitor::visitSequence(const SequencePtr& p) out << sp << nl << "public void" << nl << "patch(Ice.Object v)"; out << sb; - out << nl << "__values[__index] = (" << typeToString(p->type(), TypeModeIn, scope) << ")v;"; + string elmtType = typeToString(p->type(), TypeModeIn, scope); + if(metaData.empty()) + { + out << nl << "__values[__index] = (" << elmtType << ")v;"; + } + else + { + out << nl << "__values.set(__index, (" << elmtType << ")v);"; + } out << eb; - out << sp << nl << "private " << typeS << " __values;"; + out << sp << nl << "public String" << nl << "type()"; + out << sb; + out << nl << "return \"" << p->type()->typeId() << "\";"; + out << eb; + + out << sp << nl << "private " << listType << " __values;"; out << nl << "private int __index;"; out << eb; } @@ -2633,7 +2657,7 @@ Slice::Gen::HelperVisitor::visitSequence(const SequencePtr& p) out << sb; out << nl << typeS << " __v;"; iter = 0; - writeSequenceMarshalUnmarshalCode(out, scope, p, "__v", false, iter, false); + writeSequenceMarshalUnmarshalCode(out, scope, p, "__v", false, iter, false); out << nl << "return __v;"; out << eb; @@ -2844,9 +2868,15 @@ Slice::Gen::HelperVisitor::visitDictionary(const DictionaryPtr& p) out << sp << nl << "public void" << nl << "patch(Ice.Object v)"; out << sb; + out << nl << valueS << " _v = (" << valueS << ")v;"; out << nl << "__m.put(__key, v);"; out << eb; + out << sp << nl << "public String" << nl << "type()"; + out << sb; + out << nl << "return \"" << value->typeId() << "\";"; + out << eb; + out << sp << nl << "private java.util.Map __m;"; out << nl << "private " << keyTypeS << " __key;"; out << eb; |