[syslinux:master] core: Avoid initializing the cache more than once

syslinux-bot for Raphael S. Carvalho raphael.scarv at gmail.com
Thu Apr 3 16:39:05 PDT 2014


Commit-ID:  2bf0922e80b35c79fcc56318e71b1da29f55df0c
Gitweb:     http://www.syslinux.org/commit/2bf0922e80b35c79fcc56318e71b1da29f55df0c
Author:     Raphael S. Carvalho <raphael.scarv at gmail.com>
AuthorDate: Wed, 5 Feb 2014 17:21:37 -0500
Committer:  Gene Cumm <gene.cumm at gmail.com>
CommitDate: Thu, 3 Apr 2014 13:35:06 -0400

core: Avoid initializing the cache more than once

Most of file system drivers initialize the cache themselves.
The problem is that the same cache could be again initialized later,
then invalidating the previous one. This patch fixes this.

Problem found while auditing the code.

Signed-off-by: Raphael S. Carvalho <raphael.scarv at gmail.com>

---
 core/fs/cache.c   | 2 ++
 core/fs/diskio.c  | 1 +
 core/fs/fs.c      | 5 +++--
 core/include/fs.h | 1 +
 4 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/core/fs/cache.c b/core/fs/cache.c
index 3b21fc2..798c622 100644
--- a/core/fs/cache.c
+++ b/core/fs/cache.c
@@ -55,6 +55,8 @@ void cache_init(struct device *dev, int block_size_shift)
         data += dev->cache_block_size;
         prev = cur++;
     }
+
+    dev->cache_init = 1; /* Set cache as initialized */
 }
 
 /*
diff --git a/core/fs/diskio.c b/core/fs/diskio.c
index 7d95d67..e9a4c1d 100644
--- a/core/fs/diskio.c
+++ b/core/fs/diskio.c
@@ -28,6 +28,7 @@ struct device * device_init(void *args)
     dev.disk = firmware->disk_init(args);
     dev.cache_size = 128*1024;
     dev.cache_data = malloc(dev.cache_size);
+    dev.cache_init = 0; /* Explicitly set cache as uninitialized */
 
     return &dev;
 }
diff --git a/core/fs/fs.c b/core/fs/fs.c
index 6965d1d..0846c88 100644
--- a/core/fs/fs.c
+++ b/core/fs/fs.c
@@ -430,8 +430,9 @@ void fs_init(const struct fs_ops **ops, void *priv)
     }
     this_fs = &fs;
 
-    /* initialize the cache */
-    if (fs.fs_dev && fs.fs_dev->cache_data)
+    /* initialize the cache only if it wasn't already initialized
+     * by the fs driver */
+    if (fs.fs_dev && fs.fs_dev->cache_data && !fs.fs_dev->cache_init)
         cache_init(fs.fs_dev, blk_shift);
 
     /* start out in the root directory */
diff --git a/core/include/fs.h b/core/include/fs.h
index 22fd6bf..ba7d674 100644
--- a/core/include/fs.h
+++ b/core/include/fs.h
@@ -133,6 +133,7 @@ struct device {
     struct disk *disk;
 
     /* the cache stuff */
+    uint8_t cache_init; /* cache initialized state */
     char *cache_data;
     struct cache *cache_head;
     uint16_t cache_block_size;


More information about the Syslinux-commits mailing list