From 4c8d2baa17fb0c2bba09977e5642fef085c88115 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 1 Sep 2010 22:02:07 +0000 Subject: [PATCH] catch invalid arguments early; avoid stack overflow in object-to-collection case (SPR-7488) git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3631 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../convert/support/GenericConversionService.java | 3 ++- .../support/ObjectToCollectionConverter.java | 13 ++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java index 538df6a2457..9804ef78a3e 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java @@ -170,9 +170,10 @@ public class GenericConversionService implements ConversionService, ConverterReg logger.debug("Converted to null"); return null; } + Assert.isTrue(source == null || sourceType.getObjectType().isInstance(source)); GenericConverter converter = getConverter(sourceType, targetType); if (converter == null) { - if (source == null || targetType.getType().isInstance(source)) { + if (source == null || targetType.getObjectType().isInstance(source)) { logger.debug("No converter found - returning assignable source object as-is"); return source; } diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionConverter.java index b08b13b2920..6e9bdd94bc7 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionConverter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToCollectionConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,6 +30,7 @@ import org.springframework.core.convert.converter.ConditionalGenericConverter; * Will convert the Object to the target Collection's parameterized type if necessary. * * @author Keith Donald + * @author Juergen Hoeller * @since 3.0 */ final class ObjectToCollectionConverter implements ConditionalGenericConverter { @@ -54,8 +55,14 @@ final class ObjectToCollectionConverter implements ConditionalGenericConverter { return null; } Collection target = CollectionFactory.createCollection(targetType.getType(), 1); - Object targetElement = this.conversionService.convert(source, sourceType, targetType.getElementTypeDescriptor(source)); - target.add(targetElement); + TypeDescriptor elementType = targetType.getElementTypeDescriptor(source); + // Avoid potential recursion... + if (!Collection.class.isAssignableFrom(elementType.getType())) { + target.add(this.conversionService.convert(source, sourceType, elementType)); + } + else { + target.add(source); + } return target; }