tony2001                Wed Sep  6 11:34:43 2006 UTC

  Added files:                 (Branch: PHP_5_2)
    /php-src/ext/dbase/tests    002.phpt 

  Modified files:              
    /php-src/ext/dbase  dbf_head.c 
  Log:
  fix leaks & uninitialized vars
  add test
  
  
http://cvs.php.net/viewvc.cgi/php-src/ext/dbase/dbf_head.c?r1=1.14.4.1.2.2&r2=1.14.4.1.2.3&diff_format=u
Index: php-src/ext/dbase/dbf_head.c
diff -u php-src/ext/dbase/dbf_head.c:1.14.4.1.2.2 
php-src/ext/dbase/dbf_head.c:1.14.4.1.2.3
--- php-src/ext/dbase/dbf_head.c:1.14.4.1.2.2   Tue Aug  8 15:55:27 2006
+++ php-src/ext/dbase/dbf_head.c        Wed Sep  6 11:34:43 2006
@@ -26,10 +26,14 @@
 
        if ((dbh = (dbhead_t *)calloc(1, sizeof(dbhead_t))) == NULL)
                return NULL;
-       if (lseek(fd, 0, 0) < 0)
+       if (lseek(fd, 0, 0) < 0) {
+               free(dbh);
                return NULL;
-       if ((ret = read(fd, &dbhead, sizeof(dbhead))) < 0)
+       }
+       if ((ret = read(fd, &dbhead, sizeof(dbhead))) <= 0) {
+               free(dbh);
                return NULL;
+       }
 
        /* build in core info */
        dbh->db_fd = fd;
@@ -54,6 +58,7 @@
 
                if (gf_retval < 0) {
                        free_dbf_head(dbh);
+                       free(tdbf);
                        return NULL;
                }
                if (gf_retval != 2 ) {
@@ -89,7 +94,7 @@
                        free(cur_f->db_format);
                }
        }
-
+       
        free(dbf);
        free(dbh);
 }
@@ -119,7 +124,7 @@
 
        if (lseek(fd, 0, 0) < 0)
                return -1;
-       if ((ret = write(fd, &dbhead, sizeof(dbhead))) < 0)
+       if ((ret = write(fd, &dbhead, sizeof(dbhead))) <= 0)
                return -1;
        return ret;
 }
@@ -132,7 +137,7 @@
        struct dbf_dfield       dbfield;
        int ret;
 
-       if ((ret = read(dbh->db_fd, &dbfield, sizeof(dbfield))) < 0) {
+       if ((ret = read(dbh->db_fd, &dbfield, sizeof(dbfield))) <= 0) {
                return ret;
        }
 
@@ -192,7 +197,7 @@
        }
 
        /* now write it out to disk */
-       if ((ret = write(dbh->db_fd, &dbfield, sizeof(dbfield))) < 0) {
+       if ((ret = write(dbh->db_fd, &dbfield, sizeof(dbfield))) <= 0) {
                return ret;
        }
        return 1;
@@ -252,21 +257,14 @@
 
        cp = dp;
        if ((fd = VCWD_OPEN(cp, o_flags|O_BINARY)) < 0) {
-               cp = (char *)malloc(MAXPATHLEN);  /* So where does this get 
free()'d?  -RL */
-               strncpy(cp, dp, MAXPATHLEN-5); strcat(cp, ".dbf");
-               if ((fd = VCWD_OPEN(cp, o_flags)) < 0) {
-                       free(cp);
-                       perror("open");
-                       return NULL;
-               }
+               return NULL;
        }
 
        if ((dbh = get_dbf_head(fd)) == NULL) {
                return NULL;
        }
-       dbh->db_name = cp;
+
        dbh->db_cur_rec = 0;
-       
        return dbh;
 }
 

http://cvs.php.net/viewvc.cgi/php-src/ext/dbase/tests/002.phpt?view=markup&rev=1.1
Index: php-src/ext/dbase/tests/002.phpt
+++ php-src/ext/dbase/tests/002.phpt
--TEST--
dbase_open() tests
--SKIPIF--
<?php if (!extension_loaded("dbase")) print "skip"; ?>
--FILE--
<?php

$file = dirname(__FILE__)."/002.dbf";
@unlink($file);

$fp = fopen($file, "w");
fclose($fp);

var_dump(dbase_open($file, -1));
var_dump(dbase_open($file, 1000));
var_dump(dbase_open($file, 0));
var_dump(dbase_open($file."nonex", 0));
var_dump(dbase_open("", 0));

@unlink($file);

$def = array(
  array("date",    "D"),
  array("name",    "C",  50),
  array("age",      "N",  3, 0),
  array("email",    "C", 128),
  array("ismember", "L")
);

var_dump(dbase_create($file, $def));
var_dump(dbase_open($file, 0));

@unlink($file);

echo "Done\n";
?>
--EXPECTF--     
Warning: dbase_open(): unable to open database %s in %s on line %d
bool(false)

Warning: dbase_open(): unable to open database %s in %s on line %d
bool(false)

Warning: dbase_open(): unable to open database %s in %s on line %d
bool(false)

Warning: dbase_open(): unable to open database %s in %s on line %d
bool(false)

Warning: dbase_open(): unable to open database  in %s on line %d
bool(false)
int(%d)
int(%d)
Done

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to