Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

How to be cautious to prevent PRIVACY LEAKS when my class has array as instance variable?

Thread view: 
www - 19 Apr 2007 19:21 GMT
Hi,

My class is like following:

public MyClass
{
    private double[] _dataArray = new double[100];

    public void setDataArray(double[] inputArray)  //not good!
    {
        _dataArray = inputArray;
    }

    public double[] getDataArray()
    {
        return _dataArray;   //privacy leak
    }

}

An Eclipse plugin pointed out that both places could be causing privacy
leak. I know, in Java, array is a tricky data type. It is sort of like
Class variable. So passing array reference is like passing an object
reference: still one object, but with multiple references to it. Each
reference has the ability to modify the object.

What should I do to avoid such a potential problem?

Thank you very much.
Tom Hawtin - 19 Apr 2007 19:37 GMT
> public MyClass
> {
[quoted text clipped - 3 lines]
>     {
>         _dataArray = inputArray;
          _dataArray = inputArray.clone();
>     }
>
>     public double[] getDataArray()
>     {
>         return _dataArray;   //privacy leak
          return _dataArray.clone();
>     }
>
> }
>
> An Eclipse plugin pointed out that both places could be causing privacy
> leak. [...]

If it was a List<Double> instead of double[], then use new
ArrayList<Double>(data). Collections.unmodifiableList is also a
possibility for outputs, but the unodifiable list tracks the original list.

Tom Hawtin
www - 19 Apr 2007 19:51 GMT
>> public MyClass
>> {
[quoted text clipped - 13 lines]
>>
>> }

I just checked my Java text book. Here is the answer:

public double[] getDataArray()
{
    double temp = new double[_dataArray.length];
    for(int i=0; i<_dataArray.length; i++)
    {
        temp[i] = _dataArray[i];
    }

    return temp;
}

I am wondering which way is better, comparing to your way (return
_dataArray.clone() ). Is your way a new feature in Java?
Tom Hawtin - 19 Apr 2007 20:46 GMT
> I just checked my Java text book. Here is the answer:
> [...]
>     double temp = new double[_dataArray.length];
            ^[]

> I am wondering which way is better, comparing to your way (return
> _dataArray.clone() ). Is your way a new feature in Java?

No, it's been in there since 1.0. However for 1.4 and earlier you had to
write:

        return (double[])_dataArray.clone();

To explicitly write a loop to copy array is nuts.

Tom Hawtin
Eric Sosman - 19 Apr 2007 21:26 GMT
Tom Hawtin wrote On 04/19/07 15:46,:

>>I just checked my Java text book. Here is the answer:
>>[...]
[quoted text clipped - 11 lines]
>
> To explicitly write a loop to copy array is nuts.

   Agreed: In tests discussed on this newsgroup at the
end of February and beginning of March, new-and-loop took
about 30% longer than clone(), and almost twice as long
as new-and-arrayCopy() ...

   YMMV.

Signature

Eric.Sosman@sun.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.