1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
From: Cyril Roelandt <tipecaml@gmail.com>
Subject: allocate the exact amount of memory that is needed
diff --git a/dbdb.c b/dbdb.c
index 8742dcc..87d8266 100644
--- a/dbdb.c
+++ b/dbdb.c
@@ -178,7 +178,7 @@ dbt_t* dbdb_db_opentable( dbhdb_t* pthis, cpchar table, bool_t rdonly )
DBT key;
DBT val;
- char szpath[PATH_MAX];
+ char* szpath;
ptable = (dbtdb_t*)malloc( sizeof(dbtdb_t) );
if( ptable == NULL )
@@ -194,6 +194,11 @@ dbt_t* dbdb_db_opentable( dbhdb_t* pthis, cpchar table, bool_t rdonly )
ptable->getcount = dbdb_table_getcount;
ptable->dbp = NULL;
+ szpath = malloc( strlen ( pthis->dir ) + strlen( table ) + 5 );
+ if( szpath == NULL )
+ {
+ return NULL;
+ }
sprintf( szpath, "%s/%s.db", pthis->dir, table );
#if !defined(DB_VERSION_MAJOR)
if( (dbp = dbopen( szpath, O_CREAT|O_RDWR, 0644, DB_BTREE, NULL)) == NULL )
@@ -241,6 +246,7 @@ dbt_t* dbdb_db_opentable( dbhdb_t* pthis, cpchar table, bool_t rdonly )
bail:
free( ptable );
+ free( szpath );
return NULL;
}
diff --git a/dbtext.c b/dbtext.c
index 4914ef1..4af33f3 100644
--- a/dbtext.c
+++ b/dbtext.c
@@ -125,7 +125,7 @@ dbt_t* dbtext_db_opentable( dbhtext_t* pthis, cpchar table, bool_t rdonly )
#ifndef NOLOCK
struct flock lock;
#endif /* ndef NOLOCK */
- char szpath[PATH_MAX];
+ char* szpath = NULL;
int flags;
struct stat st;
@@ -159,6 +159,11 @@ dbt_t* dbtext_db_opentable( dbhtext_t* pthis, cpchar table, bool_t rdonly )
ptable->nitems = 0;
ptable->pitems = NULL;
+ szpath = malloc ( strlen( pthis->dir ) + strlen( table ) + 6 );
+ if( szpath == NULL )
+ {
+ goto bail;
+ }
sprintf( szpath, "%s/%s.txt", pthis->dir, table );
flags = (rdonly ? O_RDONLY|O_CREAT : O_RDWR|O_CREAT);
ptable->fd = open( szpath, flags, 0644 );
@@ -271,6 +276,10 @@ bail_uc:
ptable->fd = -1;
bail:
+ if( szpath != NULL )
+ {
+ free( szpath );
+ }
free( ptable );
return NULL;
}
|