diff --git a/app/Sketch.java b/app/Sketch.java index 93b30c678..9a7233302 100644 --- a/app/Sketch.java +++ b/app/Sketch.java @@ -170,7 +170,37 @@ public class Sketch { int hiddenCounter = 0; for (int i = 0; i < list.length; i++) { - if (list[i].endsWith(".pde")) { + // figure out the name without any extension + String base = list[i]; + // first strip off the .x items + if (base.endsWith(".x")) { + base = base.substring(0, base.length() - 2); + } + // now strip off the .pde and .java extensions + if (base.endsWith(".pde")) { + base = base.substring(0, base.length() - 4); + } + if (base.endsWith(".java")) { + base = base.substring(0, base.length() - 4); + } + + if (list[i].startsWith(".")) { + // ignoring the dot prefix files is especially important to + // ignore ._ files on macosx because they'll have binary mess + // in them which can cause a crash.. ouch. [rev 0116] + continue; + + } else if (!Sketchbook.isSanitary(base)) { + // also don't allow people to use files with invalid names, + // since on load, it would be otherwise possible to sneak in + // nasty filenames. [rev 0116] + continue; + + } else if (new File(folder, list[i]).isDirectory()) { + // don't let some wacko name a directory blah.pde or bling.java. + continue; + + } else if (list[i].endsWith(".pde")) { code[codeCounter++] = new SketchCode(list[i].substring(0, list[i].length() - 4), new File(folder, list[i]), diff --git a/app/Sketchbook.java b/app/Sketchbook.java index dfeb3e464..e48e79b6a 100644 --- a/app/Sketchbook.java +++ b/app/Sketchbook.java @@ -256,10 +256,25 @@ public class Sketchbook { /** - * Java classes are pretty limited about what you can use - * for their naming. This helper function replaces everything - * but A-Z, a-z, and 0-9 with underscores. Also disallows - * starting the sketch name with a digit. + * Return true if the name is valid for a Processing sketch. + */ + static public boolean isSanitary(String name) { + return sanitizedName(name).equals(name); + } + + + /** + * Produce a sanitized name that fits our standards for likely to work. + *
+ * Java classes have a wider range of names that are technically allowed + * (supposedly any Unicode name) than what we support. The reason for + * going more narrow is to avoid situations with text encodings and + * converting during the process of moving files between operating + * systems, i.e. uploading from a Windows machine to a Linux server, + * or reading a FAT32 partition in OS X and using a thumb drive. + * + * This helper function replaces everything but A-Z, a-z, and 0-9 with + * underscores. Also disallows starting the sketch name with a digit. */ static public String sanitizedName(String origName) { char c[] = origName.toCharArray(); @@ -279,7 +294,12 @@ public class Sketchbook { buffer.append('_'); } } - // let's not be ridiculous about the length of filenames + // let's not be ridiculous about the length of filenames. + // in fact, Mac OS 9 can handle 255 chars, though it can't really + // deal with filenames longer than 31 chars in the Finder. + // but limiting to that for sketches would mean setting the + // upper-bound on the character limit here to 25 characters + // (to handle the base name + ".class") if (buffer.length() > 63) { buffer.setLength(63); } @@ -289,7 +309,7 @@ public class Sketchbook { public String handleOpen() { // swing's file choosers are ass ugly, so we use the - // native (awt peered) dialogs instead + // native (awt peered) dialogs where possible FileDialog fd = new FileDialog(editor, //new Frame(), "Open a Processing sketch...", FileDialog.LOAD); @@ -454,8 +474,9 @@ public class Sketchbook { File entry = new File(subfolder, list[i] + ".pde"); // if a .pde file of the same prefix as the folder exists.. if (entry.exists()) { - String sanityCheck = sanitizedName(list[i]); - if (!sanityCheck.equals(list[i])) { + //String sanityCheck = sanitizedName(list[i]); + //if (!sanityCheck.equals(list[i])) { + if (!Sketchbook.isSanitary(list[i])) { if (!builtOnce) { String complaining = "The sketch \"" + list[i] + "\" cannot be used.\n" +