[Android] DirectoryAdapter: prevent stack overflow from overly deep recursion

Edward Wang git at videolan.org
Sat Mar 9 03:35:16 CET 2013


vlc-ports/android | branch: master | Edward Wang <edward.c.wang at compdigitec.com> | Fri Mar  8 21:32:18 2013 -0500| [f3c0ef0d749887af60857980072830d772161f27] | committer: Edward Wang

DirectoryAdapter: prevent stack overflow from overly deep recursion

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=f3c0ef0d749887af60857980072830d772161f27
---

 .../src/org/videolan/vlc/gui/DirectoryAdapter.java   |   18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java b/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java
index 457ea77..765b6f2 100644
--- a/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java
@@ -132,7 +132,16 @@ public class DirectoryAdapter extends BaseAdapter {
         ImageView icon;
     }
 
-    public void populateNode(DirectoryAdapter.Node n, String path) {
+    private void populateNode(DirectoryAdapter.Node n, String path) {
+        populateNode(n, path, 0);
+    }
+
+    /**
+     * @param n Node to populate
+     * @param path Path to populate
+     * @param depth Depth of iteration (0 = 1st level of nesting, 1 = 2 level of nesting, etc)
+     */
+    private void populateNode(DirectoryAdapter.Node n, String path, int depth) {
         if (path == null) {
             // We're on the storage list
             String storages[] = Util.getMediaDirectories();
@@ -140,7 +149,7 @@ public class DirectoryAdapter extends BaseAdapter {
                 File f = new File(storage);
                 DirectoryAdapter.Node child = new DirectoryAdapter.Node(f.getName(), getVisibleName(f));
                 child.isFile = false;
-                this.populateNode(child, storage);
+                this.populateNode(child, storage, 0);
                 n.children.add(child);
             }
             return;
@@ -171,7 +180,8 @@ public class DirectoryAdapter extends BaseAdapter {
                 String newPath = sb.toString();
                 sb.setLength(0);
 
-                if (LibVLC.nativeIsPathDirectory(newPath)) {
+                // Don't try to go beyond depth 10 as a safety measure.
+                if (LibVLC.nativeIsPathDirectory(newPath) && depth < 10) {
                     ArrayList<String> files_int = new ArrayList<String>();
                     LibVLC.nativeReadDirectory(newPath, files_int);
                     if(files_int.size() < 8) { /* Optimisation: If there are more than 8
@@ -179,7 +189,7 @@ public class DirectoryAdapter extends BaseAdapter {
                                                    when scaled it is very slow to load */
                         String mCurrentDir_old = mCurrentDir;
                         mCurrentDir = path;
-                        this.populateNode(nss, newPath);
+                        this.populateNode(nss, newPath, depth+1);
                         mCurrentDir = mCurrentDir_old;
                     }
                 } else {



More information about the Android mailing list