Skip to content

Commit

Permalink
Custom File Filter Null Extension Fixes (#447)
Browse files Browse the repository at this point in the history
Provide string utility method with guaranteed contract to return false instead of an NPE for null strings and suffixes. Prevent null extensions from being accepted in the custom file filter constructor.
  • Loading branch information
RobertBColton authored Jul 7, 2019
1 parent a4d49b3 commit 7ccbc50
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
6 changes: 4 additions & 2 deletions org/lateralgm/components/impl/CustomFileFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import javax.swing.filechooser.FileFilter;

import org.lateralgm.main.Util;

public class CustomFileFilter extends FileFilter implements FilenameFilter
{
private ArrayList<String> ext = new ArrayList<String>();
Expand All @@ -36,7 +38,7 @@ public CustomFileFilter(String desc, String...ext)
{
this.desc = desc;
for (String element : ext)
this.ext.add(element);
if (element != null) this.ext.add(element);
}

public boolean accept(File f)
Expand All @@ -50,7 +52,7 @@ public boolean accept(File dir, String name)
if (ext.size() == 0) return true;
//if (f.isDirectory()) return true;
for (String e : ext)
if (name.endsWith(e))
if (Util.stringEndsWith(name,e))
return true;
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion org/lateralgm/main/LGM.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@

public final class LGM
{
public static final String version = "1.8.82"; //$NON-NLS-1$
public static final String version = "1.8.83"; //$NON-NLS-1$

// TODO: This list holds the class loader for any loaded plugins which should be
// cleaned up and closed when the application closes.
Expand Down
7 changes: 7 additions & 0 deletions org/lateralgm/main/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -1255,4 +1255,11 @@ public static void OpenDesktopEditor(File file)
JOptionPane.ERROR_MESSAGE);
}
}

// Guaranteed contract to not throw an NPE and return false if str or suffix is null.
public static boolean stringEndsWith(String str, String suffix)
{
if (suffix == null || str == null) return false;
return str.endsWith(suffix);
}
}

0 comments on commit 7ccbc50

Please sign in to comment.