During a code audit I am checking the return value of File.listFiles()
is not null. Its probably expected by most programmer for an empty
array to be returned or exception however its possible for this method
to return null and in many program situations the value is not checked
and freak NullPointerExceptions can occur. There are major bugs or
security issues relating to this, I am just seeking improvement in code
quality with a couple of extra lines of code.
I have found the following code fragments inside itext that are should
check for a null return and take action:
In class com.lowagie.text.pdf.DefaultFontMapper:
public int insertDirectory(String dir) {
File file = new File(dir);
if (!file.exists() || !file.isDirectory())
return 0;
File files[] = file.listFiles();
int count = 0;
for (int k = 0; k < files.length; ++k) {
file = files[k];
String name = file.getPath().toLowerCase();
It is still possible on a modern multiuser / multitask OS for the files
to be deleted between the file.exists() check and the file.listFiles() a
simple "if(files == null) return 0;" should do the trick in the above case.
In class com.lowagie.tools.BuildTutorial:
public static void action(File source, File destination, File
xsl_examples, File xsl_site) throws IOException {
if ("CVS".equals(source.getName())) return;
System.out.print(source.getName());
if (source.isDirectory()) {
System.out.print(" ");
System.out.println(source.getCanonicalPath());
File dest = new File(destination, source.getName());
dest.mkdir();
File current;
File[] xmlFiles = source.listFiles();
for (int i = 0; i < xmlFiles.length; i++) {
current = xmlFiles[i];
action(current, dest, xsl_examples, xsl_site);
}
com.lowagie.tools.plugins.PhotoAlbum:
public void execute() {
try {
if (getValue("srcdir") == null) throw new
InstantiationException("You need to choose a source directory");
File directory = (File)getValue("srcdir");
if (directory.isFile()) directory = directory.getParentFile();
if (getValue("destfile") == null) throw new
InstantiationException("You need to choose a destination file");
File pdf_file = (File)getValue("destfile");
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new
FileOutputStream(pdf_file));
writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
PdfPageLabels pageLabels = new PdfPageLabels();
int dpiX, dpiY;
float imgWidthPica, imgHeightPica;
TreeSet images = new TreeSet();
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) images.add(files[i]);
}
Darryl
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions