[ 
https://issues.apache.org/jira/browse/FELIX-4361?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13851543#comment-13851543
 ] 

Sander Mak commented on FELIX-4361:
-----------------------------------

One trivial improvement to the patch would be to allocate an ArrayList of the 
correct capacity, to avoid resizing overhead:

> List copyComponents = new ArrayList(m_components.size());

or even let the list copy itself without for loop in the synchronized block

>  List copyComponents = new ArrayList(m_components);

> Possible ConcurrentModificationException in DependencyManager.getComponents()
> -----------------------------------------------------------------------------
>
>                 Key: FELIX-4361
>                 URL: https://issues.apache.org/jira/browse/FELIX-4361
>             Project: Felix
>          Issue Type: Bug
>          Components: Dependency Manager
>    Affects Versions: dependencymanager-3.1.0
>            Reporter: Paul Bakker
>            Assignee: Marcel Offermans
>         Attachments: FELIX-4361.patch
>
>
> DependencyManager.getComponents() returns a unmodifiableList created as 
> follows:
> {code}
> Collections.unmodifiableList(m_components);
> {code}
> However, this does not provide safe iteration on the result of calling this 
> method. E.g. the following can fail with a ConcurrentModificationException:
> {code}
> List<ComponentDeclaration> components = dm.getComponents();
> for (ComponentDeclaration c : components) {
>   //do something
> }
> {code}
> This is possible because the underlaying collection can be modified during 
> iteration. Wrapping it in an unmodifiable list doesn't prevent this, because 
> the modifications are done on the original list.
> This can be fixed by copying the list to a new collection before returning. 
> This is more expensive, but the only way to be safe.
> Patch and test provided.



--
This message was sent by Atlassian JIRA
(v6.1.4#6159)

Reply via email to