Commit 203500a3 authored by Yoni Fogel's avatar Yoni Fogel

Addresses #583

We were reading error codes of malloc incorrectly.
errno is not valid unless you know you had an error.

git-svn-id: file:///svn/tokudb@3138 c7de825b-a66e-492c-adef-691d508d4ae1
parent 6e741a19
...@@ -17,26 +17,34 @@ DBT *toku_fill_dbt(DBT *dbt, bytevec k, ITEMLEN len) { ...@@ -17,26 +17,34 @@ DBT *toku_fill_dbt(DBT *dbt, bytevec k, ITEMLEN len) {
} }
int toku_dbt_set_value (DBT *ybt, bytevec val, ITEMLEN vallen, void **staticptrp) { int toku_dbt_set_value (DBT *ybt, bytevec val, ITEMLEN vallen, void **staticptrp) {
int r = ENOSYS;
if (ybt->flags==DB_DBT_MALLOC) { if (ybt->flags==DB_DBT_MALLOC) {
domalloc: domalloc:
ybt->data = toku_malloc(vallen); ybt->data = toku_malloc(vallen);
if (errno!=0) return errno; if (!ybt->data && vallen > 0) { r = errno; goto cleanup; }
} else if (ybt->flags==DB_DBT_REALLOC) { } else if (ybt->flags==DB_DBT_REALLOC) {
if (ybt->data==0) goto domalloc; if (ybt->data==0) goto domalloc;
ybt->data = toku_realloc(ybt->data, vallen); /* tmp is used to prevent a memory leak if realloc fails */
if (errno!=0) return errno; void* tmp = toku_realloc(ybt->data, vallen);
if (!tmp && vallen > 0) { r = errno; goto cleanup; }
ybt->data = tmp;
} else if (ybt->flags==DB_DBT_USERMEM) { } else if (ybt->flags==DB_DBT_USERMEM) {
ybt->size = vallen; ybt->size = vallen;
if (ybt->ulen < vallen) return DB_BUFFER_SMALL; if (ybt->ulen < vallen) { r = DB_BUFFER_SMALL; goto cleanup; }
} else { } else {
if (staticptrp==0) return -1; if (staticptrp==0) return -1;
void *staticptr=*staticptrp; void *staticptr=*staticptrp;
//void *old=staticptr; //void *old=staticptr;
if (staticptr==0) if (staticptr==0) {
staticptr = toku_malloc(vallen); staticptr = toku_malloc(vallen);
else if (!staticptr && vallen > 0) { r = errno; goto cleanup; }
staticptr = toku_realloc(staticptr, vallen); }
if (errno!=0) return errno; else {
/* tmp is used to prevent a memory leak if realloc fails */
void* tmp = toku_realloc(staticptr, vallen);
if (!tmp && vallen > 0) { r = errno; goto cleanup; }
staticptr = tmp;
}
//if (old!=staticptr) printf("%s:%d MALLOC --> %p\n", __FILE__, __LINE__, staticptr); //if (old!=staticptr) printf("%s:%d MALLOC --> %p\n", __FILE__, __LINE__, staticptr);
*staticptrp = staticptr; *staticptrp = staticptr;
ybt->data = vallen > 0 ? staticptr : 0; ybt->data = vallen > 0 ? staticptr : 0;
...@@ -45,5 +53,8 @@ int toku_dbt_set_value (DBT *ybt, bytevec val, ITEMLEN vallen, void **staticptrp ...@@ -45,5 +53,8 @@ int toku_dbt_set_value (DBT *ybt, bytevec val, ITEMLEN vallen, void **staticptrp
if (ybt->size>0) { if (ybt->size>0) {
memcpy(ybt->data, val, vallen); memcpy(ybt->data, val, vallen);
} }
return 0; r = 0;
cleanup:
return r;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment