php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47002 Fields truncated
Submitted: 2009-01-04 22:17 UTC Modified: 2009-12-23 04:16 UTC
From: victorjavierss at live dot com dot mx Assigned:
Status: Closed Package: dBase related
PHP Version: 5.2CVS-2009-01-04 (snap) OS: Windows
Private report: No CVE-ID:
 [2009-01-04 22:17 UTC] victorjavierss at live dot com dot mx
Description:
------------
I'm executing the code belong, and it supose to retrieve me 1625 fields, but it doesn't, it give me 1024 fields, also i tried with other dbf's bigger than 1024 and the problem stills there



Reproduce code:
---------------
<?php
if ($db = dbase_open('C:\SIDEC\SIDEC-I\ARCHIVOS\USAERI07.dbf', 0)) {
	  $rn = dbase_numfields($db);
	  echo($rn);
	  dbase_close($db);
	}
?>

Expected result:
----------------
1625

Actual result:
--------------
1024

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-01-11 18:50 UTC] typoon at gmail dot com
I was unable to find a free version of dBase so I can't really create the file here to simulate the issue.
Could you please post a link to a .dbf file with more than 1024 fields for me please? Put like 3 or 4 records as examples on it, just so I can do all tests.

Thanks!

Henrique
 [2009-01-11 18:59 UTC] victorjavierss at live dot com dot mx
Here is one of the DBF's that i'm using:

http://cid-d95738776c6cf0d0.skydrive.live.com/self.aspx/.Public/USAERF07.DBF

as I said it retrieves me 1024 fild instead of 1625
 [2009-01-12 14:09 UTC] typoon at gmail dot com
Victor,

Are you sure this file has 1625 fields? I opened the file with DBF Viewer and I only see 1100 fields. Also I changed the logic a little bit to calculate the number of fields based on the header length and I also obtain 1100 fields.
Can you please confirm the number of fields in the file? Also, what is the version of dbase you are using?

Thanks!
 [2009-01-13 05:59 UTC] victorjavierss at live dot com dot mx
Ooops, i got confused with other dbf, that i'm using, but that file is over 1024 fields and  y need all of them
 [2009-01-13 06:01 UTC] victorjavierss at live dot com dot mx
I'm guess is dBase IV
 [2009-08-20 16:44 UTC] sjoerd-php at linuxonly dot nl
get_dbf_head assumes a max of 1024 fields. Below is a patch which removes the 1024 limit and resizes the memory buffer if there are more than 1024 fields.

--- 5.2latest/php5.2-200906261830/ext/dbase/dbf_head.c	2009-01-17 18:45:09.000000000 +0100
+++ php-5.2.10/ext/dbase/dbf_head.c	2009-08-20 18:37:56.000000000 +0200
@@ -22,7 +22,7 @@
 	dbhead_t *dbh;
 	struct dbf_dhead  dbhead;
 	dbfield_t *dbf, *cur_f, *tdbf;
-	int ret, nfields, offset, gf_retval;
+	int ret, nfields, offset, gf_retval, cur_f_offset, tdbf_size;
 
 	if ((dbh = (dbhead_t *)calloc(1, sizeof(dbhead_t))) == NULL)
 		return NULL;
@@ -46,14 +46,14 @@
 		dbhead.dbh_date[DBH_DATE_MONTH],
 		dbhead.dbh_date[DBH_DATE_DAY]);
 
-	/* malloc enough memory for the maximum number of fields:
-	   32 * 1024 = 32K dBase5 (for Win) seems to allow that many */
-	tdbf = (dbfield_t *)calloc(1, sizeof(dbfield_t)*1024);
+	/* Although 1024 fields used to be the max, bug 47002 reports more than 1024 fields. */
+	tdbf_size = 1024;
+	tdbf = (dbfield_t *)calloc(1, sizeof(dbfield_t)*tdbf_size);
 	
 	offset = 1;
 	nfields = 0;
 	gf_retval = 0;
-	for (cur_f = tdbf; gf_retval < 2 && nfields < 1024; cur_f++) {
+	for (cur_f = tdbf; gf_retval < 2; cur_f++) {
 		gf_retval = get_dbf_field(dbh, cur_f);
 
 		if (gf_retval < 0) {
@@ -66,6 +66,13 @@
 			offset += cur_f->db_flen;
 			nfields++;
 		}
+		if (nfields >= tdbf_size) {
+			cur_f_offset = cur_f - tdbf;
+			tdbf = realloc(tdbf, sizeof(dbfield_t) * tdbf_size * 2);
+			memset(tdbf + tdbf_size, '\0', tdbf_size);
+			tdbf_size *= 2;
+			cur_f = tdbf + cur_f_offset;
+		}
 	}
 	dbh->db_nfields = nfields;
 [2009-08-31 16:35 UTC] sjoerd@php.net
Could reproduce and code shows it gets only 1024 fields.
 [2009-12-23 04:16 UTC] svn@php.net
Automatic comment from SVN on behalf of iliaa
Revision: http://svn.php.net/viewvc/?view=revision&revision=292514
Log: Fixed bug #47002 (Field truncation when reading from dbase dbs with more then 1024 fields)
 [2009-12-23 04:16 UTC] iliaa@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Sat Apr 19 01:01:59 2014 UTC