Bug 348706 - Passing null as a value of a DOM attribute causes crash. r=vkorenv. XULRunner only.

This commit is contained in:
pedemont%us.ibm.com 2006-10-06 15:15:46 +00:00
Родитель c82ed5066a
Коммит 5009e695bb
4 изменённых файлов: 124 добавлений и 227 удалений

Просмотреть файл

@ -782,35 +782,22 @@ SetupParams(JNIEnv *env, const jobject aParam, PRUint8 aType, PRBool aIsOut,
case nsXPTType::T_DOMSTRING:
{
LOG(("String\n"));
jstring data = nsnull;
if (!aIsOut && !aIsArrayElement) { // 'in'
data = (jstring) aParam;
} else if (aParam) { // 'inout' & 'array'
data = (jstring) env->GetObjectArrayElement((jobjectArray) aParam,
aIndex);
// Expecting only 'in' and 'in dipper'
NS_PRECONDITION(!aIsOut, "unexpected param descriptor");
if (aIsOut) {
rv = NS_ERROR_UNEXPECTED;
break;
}
nsAString* str;
if (data) {
str = jstring_to_nsAString(env, data);
jstring jstr = NS_STATIC_CAST(jstring, aParam);
nsAString* str = jstring_to_nsAString(env, jstr);
if (!str) {
rv = NS_ERROR_OUT_OF_MEMORY;
break;
}
} else {
str = nsnull;
}
if (!aIsArrayElement) { // 'in' & 'inout'
aVariant.val.p = str;
aVariant.SetValIsDOMString();
if (aIsOut) { // 'inout'
aVariant.ptr = &aVariant.val;
aVariant.SetPtrIsData();
}
} else { // 'array'
NS_STATIC_CAST(nsAString**, aVariant.val.p)[aIndex] = str;
}
break;
}
@ -818,39 +805,26 @@ SetupParams(JNIEnv *env, const jobject aParam, PRUint8 aType, PRBool aIsOut,
case nsXPTType::T_CSTRING:
{
LOG(("StringUTF\n"));
jstring data = nsnull;
if (!aIsOut && !aIsArrayElement) { // 'in'
data = (jstring) aParam;
} else if (aParam) { // 'inout' & 'array'
data = (jstring) env->GetObjectArrayElement((jobjectArray) aParam,
aIndex);
// Expecting only 'in' and 'in dipper'
NS_PRECONDITION(!aIsOut, "unexpected param descriptor");
if (aIsOut) {
rv = NS_ERROR_UNEXPECTED;
break;
}
nsACString* str;
if (data) {
str = jstring_to_nsACString(env, data);
jstring jstr = NS_STATIC_CAST(jstring, aParam);
nsACString* str = jstring_to_nsACString(env, jstr);
if (!str) {
rv = NS_ERROR_OUT_OF_MEMORY;
break;
}
} else {
str = nsnull;
}
if (!aIsArrayElement) { // 'in' & 'inout'
aVariant.val.p = str;
if (aType == nsXPTType::T_CSTRING) {
aVariant.SetValIsCString();
} else {
aVariant.SetValIsUTF8String();
}
if (aIsOut) { // 'inout'
aVariant.ptr = &aVariant.val;
aVariant.SetPtrIsData();
}
} else { // 'array'
NS_STATIC_CAST(nsACString**, aVariant.val.p)[aIndex] = str;
}
break;
}
@ -1157,14 +1131,17 @@ FinalizeParams(JNIEnv *env, const nsXPTParamInfo &aParamInfo, PRUint8 aType,
case nsXPTType::T_ASTRING:
case nsXPTType::T_DOMSTRING:
{
nsString* str = NS_STATIC_CAST(nsString*, aVariant.val.p);
NS_PRECONDITION(aParamInfo.IsIn(), "unexpected param descriptor");
if (!aParamInfo.IsIn()) {
rv = NS_ERROR_UNEXPECTED;
break;
}
if ((aParamInfo.IsOut() || aParamInfo.IsDipper() || aIsArrayElement) &&
NS_SUCCEEDED(aInvokeResult))
{
nsString* str = NS_STATIC_CAST(nsString*, aVariant.val.p);
if (NS_SUCCEEDED(aInvokeResult) && aParamInfo.IsDipper()) {
// Create Java string from returned nsString
jstring jstr = nsnull;
if (str) {
if (str && !str->IsVoid()) {
jstr = env->NewString((const jchar*) str->get(), str->Length());
if (!jstr) {
rv = NS_ERROR_OUT_OF_MEMORY;
@ -1172,12 +1149,7 @@ FinalizeParams(JNIEnv *env, const nsXPTParamInfo &aParamInfo, PRUint8 aType,
}
}
if (aParamInfo.IsRetval() && !aIsArrayElement) {
*aParam = jstr;
} else if (*aParam) {
// put new Java string into output array
env->SetObjectArrayElement((jobjectArray) *aParam, aIndex, jstr);
}
}
// cleanup
@ -1190,14 +1162,17 @@ FinalizeParams(JNIEnv *env, const nsXPTParamInfo &aParamInfo, PRUint8 aType,
case nsXPTType::T_UTF8STRING:
case nsXPTType::T_CSTRING:
{
nsCString* str = NS_STATIC_CAST(nsCString*, aVariant.val.p);
NS_PRECONDITION(aParamInfo.IsIn(), "unexpected param descriptor");
if (!aParamInfo.IsIn()) {
rv = NS_ERROR_UNEXPECTED;
break;
}
if ((aParamInfo.IsOut() || aParamInfo.IsDipper() || aIsArrayElement) &&
NS_SUCCEEDED(aInvokeResult))
{
nsCString* str = NS_STATIC_CAST(nsCString*, aVariant.val.p);
if (NS_SUCCEEDED(aInvokeResult) && aParamInfo.IsDipper()) {
// Create Java string from returned nsString
jstring jstr = nsnull;
if (str) {
if (str && !str->IsVoid()) {
jstr = env->NewStringUTF((const char*) str->get());
if (!jstr) {
rv = NS_ERROR_OUT_OF_MEMORY;
@ -1205,12 +1180,7 @@ FinalizeParams(JNIEnv *env, const nsXPTParamInfo &aParamInfo, PRUint8 aType,
}
}
if (aParamInfo.IsRetval() && !aIsArrayElement) {
*aParam = jstr;
} else if (*aParam) {
// put new Java string into output array
env->SetObjectArrayElement((jobjectArray) *aParam, aIndex, jstr);
}
}
// cleanup
@ -1441,7 +1411,7 @@ JAVAPROXY_NATIVE(callXPCOMMethod) (JNIEnv *env, jclass that, jobject aJavaProxy,
const nsXPTParamInfo &paramInfo = methodInfo->GetParam(i);
params[i].type = paramInfo.GetType();
if (paramInfo.IsIn() && !paramInfo.IsDipper()) {
if (paramInfo.IsIn()) {
PRUint8 type = params[i].type.TagPart();
// is paramater an array?
@ -1478,41 +1448,12 @@ JAVAPROXY_NATIVE(callXPCOMMethod) (JNIEnv *env, jclass that, jobject aJavaProxy,
}
if (NS_SUCCEEDED(rv)) {
rv = SetupParams(env, env->GetObjectArrayElement(aParams, i), type,
paramInfo.IsOut(), iid, arrayType, arraySize,
PR_FALSE, 0, params[i]);
jobject param = nsnull;
if (aParams && !paramInfo.IsRetval()) {
param = env->GetObjectArrayElement(aParams, i);
}
} else if (paramInfo.IsDipper()) {
LOG(("dipper\n"));
switch (params[i].type.TagPart())
{
case nsXPTType::T_ASTRING:
case nsXPTType::T_DOMSTRING:
{
params[i].val.p = new nsString();
if (params[i].val.p == nsnull) {
rv = NS_ERROR_OUT_OF_MEMORY;
break;
}
params[i].SetValIsDOMString();
break;
}
case nsXPTType::T_UTF8STRING:
case nsXPTType::T_CSTRING:
{
params[i].val.p = new nsCString();
if (params[i].val.p == nsnull) {
rv = NS_ERROR_OUT_OF_MEMORY;
break;
}
params[i].SetValIsCString();
break;
}
default:
LOG(("unhandled dipper type\n"));
rv = NS_ERROR_UNEXPECTED;
rv = SetupParams(env, param, type, paramInfo.IsOut(), iid, arrayType,
arraySize, PR_FALSE, 0, params[i]);
}
} else {
LOG(("out/retval\n"));

Просмотреть файл

@ -1057,7 +1057,12 @@ jstring_to_nsAString(JNIEnv* env, jstring aString)
}
nsString* str = new nsString(buf);
if (aString) {
env->ReleaseStringChars(aString, buf);
} else {
str->SetIsVoid(PR_TRUE);
}
// returns string, or nsnull if 'new' failed
return str;
@ -1074,7 +1079,12 @@ jstring_to_nsACString(JNIEnv* env, jstring aString)
}
nsCString* str = new nsCString(buf);
if (aString) {
env->ReleaseStringUTFChars(aString, buf);
} else {
str->SetIsVoid(PR_TRUE);
}
// returns string, or nsnull if 'new' failed
return str;

Просмотреть файл

@ -371,8 +371,9 @@ void ThrowException(JNIEnv* env, const nsresult aErrorCode,
* @param env Java environment pointer
* @param aString Java string to convert
*
* @return nsAString/nsACString with same content as given Java string; or
* <code>nsnull</code> if out of memory
* @return nsAString/nsACString with same content as given Java string;
* a 'void' nsAString/nsACString object if aString is
* <code>null</code>; or <code>nsnull</code> if out of memory
*/
nsAString* jstring_to_nsAString(JNIEnv* env, jstring aString);
nsACString* jstring_to_nsACString(JNIEnv* env, jstring aString);

Просмотреть файл

@ -414,7 +414,6 @@ nsJavaXPTCStub::CallMethod(PRUint16 aMethodIndex,
for (PRUint8 i = 0; i < paramCount && NS_SUCCEEDED(rv); i++)
{
const nsXPTParamInfo &paramInfo = aMethodInfo->GetParam(i);
NS_ASSERTION(!paramInfo.IsDipper(), "Dipper!");
if (!paramInfo.IsRetval()) {
rv = SetupJavaParams(paramInfo, aMethodInfo, aMethodIndex, aParams,
aParams[i], java_params[i], methodSig);
@ -560,7 +559,7 @@ nsJavaXPTCStub::CallMethod(PRUint16 aMethodIndex,
for (PRUint8 i = 0; i < paramCount; i++)
{
const nsXPTParamInfo &paramInfo = aMethodInfo->GetParam(i);
if (paramInfo.IsIn() && !paramInfo.IsOut()) // 'in'
if (paramInfo.IsIn() && !paramInfo.IsOut() && !paramInfo.IsDipper()) // 'in'
continue;
// If param is null, then caller is not expecting an output value.
@ -960,80 +959,62 @@ nsJavaXPTCStub::SetupJavaParams(const nsXPTParamInfo &aParamInfo,
case nsXPTType::T_ASTRING:
case nsXPTType::T_DOMSTRING:
{
nsString* str = nsnull;
if (!aParamInfo.IsOut()) { // 'in'
str = NS_STATIC_CAST(nsString*, aVariant.val.p);
} else if (aVariant.val.p) { // 'inout' & 'out'
nsString** variant = NS_STATIC_CAST(nsString**, aVariant.val.p);
str = *variant;
// This only handle 'in' or 'in dipper' params. In XPIDL, the 'out'
// descriptor is mapped to 'in dipper'.
NS_PRECONDITION(aParamInfo.IsIn(), "unexpected param descriptor");
if (!aParamInfo.IsIn()) {
rv = NS_ERROR_UNEXPECTED;
break;
}
jstring jstr;
if (str) {
nsString* str = NS_STATIC_CAST(nsString*, aVariant.val.p);
if (!str) {
rv = NS_ERROR_FAILURE;
break;
}
jstring jstr = nsnull;
if (!str->IsVoid()) {
jstr = env->NewString(str->get(), str->Length());
if (!jstr) {
rv = NS_ERROR_OUT_OF_MEMORY;
break;
}
} else {
jstr = nsnull;
}
if (!aParamInfo.IsOut()) { // 'in'
aJValue.l = jstr;
aMethodSig.AppendLiteral("Ljava/lang/String;");
} else { // 'inout' & 'out'
if (aVariant.val.p) {
aJValue.l = env->NewObjectArray(1, stringClass, jstr);
if (aJValue.l == nsnull) {
rv = NS_ERROR_OUT_OF_MEMORY;
break;
}
} else {
aJValue.l = nsnull;
}
aMethodSig.AppendLiteral("[Ljava/lang/String;");
}
}
break;
case nsXPTType::T_UTF8STRING:
case nsXPTType::T_CSTRING:
{
nsCString* str = nsnull;
if (!aParamInfo.IsOut()) { // 'in'
str = NS_STATIC_CAST(nsCString*, aVariant.val.p);
} else if (aVariant.val.p) { // 'inout' & 'out'
nsCString** variant = NS_STATIC_CAST(nsCString**, aVariant.val.p);
str = *variant;
// This only handle 'in' or 'in dipper' params. In XPIDL, the 'out'
// descriptor is mapped to 'in dipper'.
NS_PRECONDITION(aParamInfo.IsIn(), "unexpected param descriptor");
if (!aParamInfo.IsIn()) {
rv = NS_ERROR_UNEXPECTED;
break;
}
jstring jstr;
if (str) {
nsCString* str = NS_STATIC_CAST(nsCString*, aVariant.val.p);
if (!str) {
rv = NS_ERROR_FAILURE;
break;
}
jstring jstr = nsnull;
if (!str->IsVoid()) {
jstr = env->NewStringUTF(str->get());
if (!jstr) {
rv = NS_ERROR_OUT_OF_MEMORY;
break;
}
} else {
jstr = nsnull;
}
if (!aParamInfo.IsOut()) { // 'in'
aJValue.l = jstr;
aMethodSig.AppendLiteral("Ljava/lang/String;");
} else { // 'inout' & 'out'
if (aVariant.val.p) {
aJValue.l = env->NewObjectArray(1, stringClass, jstr);
if (aJValue.l == nsnull) {
rv = NS_ERROR_OUT_OF_MEMORY;
break;
}
} else {
aJValue.l = nsnull;
}
aMethodSig.AppendLiteral("[Ljava/lang/String;");
}
}
break;
@ -1552,47 +1533,29 @@ nsJavaXPTCStub::FinalizeJavaParams(const nsXPTParamInfo &aParamInfo,
case nsXPTType::T_ASTRING:
case nsXPTType::T_DOMSTRING:
{
jstring str = nsnull;
if (aParamInfo.IsRetval()) { // 'retval'
str = (jstring) aJValue.l;
} else { // 'inout' & 'out'
str = (jstring) env->GetObjectArrayElement((jobjectArray) aJValue.l, 0);
NS_PRECONDITION(aParamInfo.IsDipper(), "string argument is not dipper");
if (!aParamInfo.IsDipper()) {
rv = NS_ERROR_UNEXPECTED;
break;
}
nsString** variant = NS_STATIC_CAST(nsString**, aVariant.val.p);
if (str) {
jstring jstr = (jstring) aJValue.l;
nsString* variant = NS_STATIC_CAST(nsString*, aVariant.val.p);
if (jstr) {
// Get string buffer
const jchar* wchar_ptr = env->GetStringChars(str, nsnull);
const jchar* wchar_ptr = env->GetStringChars(jstr, nsnull);
if (!wchar_ptr) {
rv = NS_ERROR_OUT_OF_MEMORY;
break;
}
if (!aParamInfo.IsRetval() && *variant) {
// If we were given an nsString, set it to the new string
nsString* string = *variant;
string->Assign(wchar_ptr);
} else {
// If the argument that was passed in was null, then we need to
// create a new string.
nsString* embedStr = new nsString(wchar_ptr);
if (embedStr) {
*variant = embedStr;
} else {
rv = NS_ERROR_OUT_OF_MEMORY;
// don't 'break'; fall through to release chars
}
}
variant->Assign(wchar_ptr);
// release String buffer
env->ReleaseStringChars(str, wchar_ptr);
env->ReleaseStringChars(jstr, wchar_ptr);
} else {
// If we were passed in a string, delete it now, and set to null.
// (Free only 'inout' & 'out' params)
if (*variant && !aParamInfo.IsRetval()) {
delete *variant;
}
*variant = nsnull;
variant->SetIsVoid(PR_TRUE);
}
}
break;
@ -1600,47 +1563,29 @@ nsJavaXPTCStub::FinalizeJavaParams(const nsXPTParamInfo &aParamInfo,
case nsXPTType::T_UTF8STRING:
case nsXPTType::T_CSTRING:
{
jstring str = nsnull;
if (aParamInfo.IsRetval()) { // 'retval'
str = (jstring) aJValue.l;
} else { // 'inout' & 'out'
str = (jstring) env->GetObjectArrayElement((jobjectArray) aJValue.l, 0);
NS_PRECONDITION(aParamInfo.IsDipper(), "string argument is not dipper");
if (!aParamInfo.IsDipper()) {
rv = NS_ERROR_UNEXPECTED;
break;
}
nsCString** variant = NS_STATIC_CAST(nsCString**, aVariant.val.p);
if (str) {
jstring jstr = (jstring) aJValue.l;
nsCString* variant = NS_STATIC_CAST(nsCString*, aVariant.val.p);
if (jstr) {
// Get string buffer
const char* char_ptr = env->GetStringUTFChars(str, nsnull);
const char* char_ptr = env->GetStringUTFChars(jstr, nsnull);
if (!char_ptr) {
rv = NS_ERROR_OUT_OF_MEMORY;
break;
}
if (!aParamInfo.IsRetval() && *variant) {
// If we were given an nsString, set it to the new string
nsCString* string = *variant;
string->Assign(char_ptr);
} else {
// If the argument that was passed in was null, then we need to
// create a new nsID.
nsCString* embedStr = new nsCString(char_ptr);
if (embedStr) {
*variant = embedStr;
} else {
rv = NS_ERROR_OUT_OF_MEMORY;
// don't 'break'; fall through to release chars
}
}
variant->Assign(char_ptr);
// release String buffer
env->ReleaseStringUTFChars(str, char_ptr);
env->ReleaseStringUTFChars(jstr, char_ptr);
} else {
// If we were passed in a string, delete it now, and set to null.
// (Free only 'inout' & 'out' params)
if (*variant && !aParamInfo.IsRetval()) {
delete *variant;
}
*variant = nsnull;
variant->SetIsVoid(PR_TRUE);
}
}
break;