harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Hudson (JIRA)" <j...@apache.org>
Subject [jira] Commented: (HARMONY-6661) Synchonrize on mutable field in Permissions.java
Date Wed, 15 Dec 2010 16:16:05 GMT

    [ https://issues.apache.org/jira/browse/HARMONY-6661?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12971723#action_12971723
] 

Hudson commented on HARMONY-6661:
---------------------------------

Integrated in Harmony-1.5-head-linux-x86_64 #1024 (See [https://hudson.apache.org/hudson/job/Harmony-1.5-head-linux-x86_64/1024/])
    

> Synchonrize on mutable field in Permissions.java
> ------------------------------------------------
>
>                 Key: HARMONY-6661
>                 URL: https://issues.apache.org/jira/browse/HARMONY-6661
>             Project: Harmony
>          Issue Type: Bug
>          Components: Classlib
>    Affects Versions: 6.0M1
>         Environment: Windows XP
>            Reporter: Wendy Feng
>            Assignee: Ray Chen
>             Fix For: 5.0M16
>
>   Original Estimate: 5h
>  Remaining Estimate: 5h
>
> I found a unsafe synchronization in modules/security/src/main/java/common/java/security/Permissions.java
> public final class Permissions extends PermissionCollection implements Serializable {
> ...
> private void readObject(java.io.ObjectInputStream in) throws IOException,
>         ClassNotFoundException {
> ...
>         klasses = new HashMap();
>         synchronized (klasses) {
>             for (Iterator iter = perms.entrySet().iterator(); iter.hasNext();) {
>                 Map.Entry entry = (Map.Entry)  iter.next();
>                 Class key = (Class) entry.getKey();
>                 PermissionCollection pc = (PermissionCollection) entry.getValue();
>                 if (key != pc.elements().nextElement().getClass()) {
>                     throw new InvalidObjectException(Messages.getString("security.22"));
//$NON-NLS-1$
>                 }
>                 klasses.put(key, pc);
>             }
>         }
>     ...
>   }
> ...
> }
> In the above code , a block is synchronized on klasses field. Before the synchronized
block, klasses is assigned to a new value.
> Consequence: 
> Different threads will synchronize on different klasses objects because it has been assigned
to a new value. It  breaks the mutual exclusion and update on klasses would be lost.
> I suggest to rewrite it as follow:
> public final class Permissions extends PermissionCollection implements Serializable {
>    private static final Object monitor = new Object();
> ...
> private void readObject(java.io.ObjectInputStream in) throws IOException,
>         ClassNotFoundException {
> ...
>         klasses = new HashMap();
>         synchronized (monitor ) {
>             for (Iterator iter = perms.entrySet().iterator(); iter.hasNext();) {
>                 Map.Entry entry = (Map.Entry)  iter.next();
>                 Class key = (Class) entry.getKey();
>                 PermissionCollection pc = (PermissionCollection) entry.getValue();
>                 if (key != pc.elements().nextElement().getClass()) {
>                     throw new InvalidObjectException(Messages.getString("security.22"));
//$NON-NLS-1$
>                 }
>                 klasses.put(key, pc);
>             }
>         }
>     ...
>   }
> ...
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message