Ignore:
Timestamp:
24/06/10 11:46:44 (2 years ago)
Author:
Tobias Richter
Message:

refs #221 add safety check for negative dims

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bindings/java/org/nexusformat/NexusFile.java

    r1474 r1486  
    311311        if(handle < 0) throw new NexusException("NAPI-ERROR: File not open"); 
    312312        checkType(type);     
    313         checkForNull(name); 
     313        checkForNull(name, rank, iChunk); 
     314        checkForNegInIntArray(true, dim, iChunk); 
    314315        switch(compression_type) { 
    315316        case NexusFile.NX_COMP_NONE: 
     
    320321 
    321322        } 
    322         nxmakecompdata(handle,name,type,rank,dim,compression_type, iChunk); 
     323        nxmakecompdata(handle, name, type, rank, dim, compression_type, iChunk); 
    323324    } 
    324325 
     
    340341        if(handle < 0) throw new NexusException("NAPI-ERROR: File not open"); 
    341342        checkType(type); 
    342         checkForNull(name); 
    343         nxmakedata(handle,name,type,rank,dim); 
     343        checkForNull(name, dim); 
     344        checkForNegInIntArray(true, dim); 
     345        nxmakedata(handle, name, type, rank, dim); 
    344346    } 
    345347    /** 
     
    442444        byte bdata[]; 
    443445        if(handle < 0) throw new NexusException("NAPI-ERROR: File not open"); 
    444         checkForNull(array); 
     446        checkForNull(start, size, array); 
     447        checkForNegInIntArray(false, start, size); 
    445448        try{ 
    446449            HDFArray ha = new HDFArray(array); 
     
    529532 
    530533       if(handle < 0) throw new NexusException("NAPI-ERROR: File not open"); 
    531        checkForNull(array); 
     534       checkForNull(array, start, size); 
     535       checkForNegInIntArray(false, start,size); 
    532536       try{ 
    533537           HDFArray ha =  new HDFArray(array); 
     
    741745    } 
    742746     
     747    /** 
     748     * checks if any of the ints in the arrays are negative,  
     749     * throws appropriate runtime exception if so 
     750     * for some being zero is stupid, but hopefully not fatal 
     751     */ 
     752    private void checkForNegInIntArray(boolean allowUnlimited, int[]... args) { 
     753        boolean first=true; 
     754        for (int[] array : args) 
     755                for (int value: array) { 
     756                        if (value<0) 
     757                                if (value == this.NX_UNLIMITED && allowUnlimited && first) { 
     758                                        // all ok this time 
     759                                } else 
     760                                        throw new IllegalArgumentException("negative dimension received"); 
     761                        first=false; 
     762                } 
     763    } 
    743764    /** 
    744765      * checkType verifies if a parameter is a valid NeXus type code.  
Note: See TracChangeset for help on using the changeset viewer.