]> git.draconx.ca Git - liblbx.git/commitdiff
liblbx: Make a copy of the string passed to lbx_open.
authorNick Bowler <nbowler@draconx.ca>
Sat, 6 Feb 2010 18:59:26 +0000 (13:59 -0500)
committerNick Bowler <nbowler@draconx.ca>
Mon, 8 Feb 2010 17:00:55 +0000 (12:00 -0500)
src/lbx.c

index 77a150a7c229c5b65bdc237e5e79d2238aca540f..05da0b4775fb74a858846d068e9b228b52bbff87 100644 (file)
--- a/src/lbx.c
+++ b/src/lbx.c
@@ -36,7 +36,7 @@
 int lbx_errno = 0;
 
 struct lbx_state {
-       const char *name;
+       char *name;
 
        const struct lbx_file_ops *fops;
        int (*dtor)(void *handle);
@@ -79,23 +79,40 @@ static struct lbx_state *lbx_init(unsigned char hdr[static LBX_HDR_SIZE])
        return lbx;
 }
 
+static char *str_dup(const char *s)
+{
+       char *buf;
+
+       buf = malloc(strlen(s)+1);
+       if (buf)
+               strcpy(buf, s);
+       return buf;
+}
+
 struct lbx_state *lbx_open(void *f, const struct lbx_file_ops *fops,
                            int (*destructor)(void *), const char *name)
 {
        unsigned char hdr_buf[LBX_HDR_SIZE];
-       struct lbx_state *lbx;
+       struct lbx_state *lbx = NULL;
+       char *dupname = NULL;
+
+       dupname = str_dup(name);
+       if (!dupname) {
+               lbx_errno = -errno;
+               goto err;
+       }
 
        if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
                lbx_errno = -errno;
-               return NULL;
+               goto err;
        }
 
        lbx = lbx_init(hdr_buf);
        if (!lbx)
-               return NULL;
+               goto err;
 
+       lbx->name = dupname;
        lbx->dtor = destructor;
-       lbx->name = name;
        lbx->fops = fops;
        lbx->f    = f;
 
@@ -106,14 +123,17 @@ struct lbx_state *lbx_open(void *f, const struct lbx_file_ops *fops,
                        lbx_errno = -errno;
                        if (fops->eof(f))
                                lbx_errno = LBX_EEOF;
-                       free(lbx);
-                       return NULL;
+                       goto err;
                }
 
                lbx->offsets[i] = unpack_32_le(buf);
        }
 
        return lbx;
+err:
+       free(dupname);
+       free(lbx);
+       return NULL;
 }
 
 struct lbx_state *lbx_fopen(FILE *f, const char *name)
@@ -148,6 +168,7 @@ int lbx_close(struct lbx_state *lbx)
 
        if (lbx && lbx->dtor)
                rc = lbx->dtor(lbx->f);
+       free(lbx->name);
        free(lbx);
 
        return rc;