Mercurial > hg > sidinfo
comparison sidlib.c @ 331:e35e15f07c66
Add bounds checks for node index generation.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 13 Jan 2020 13:11:29 +0200 |
parents | 6d0143e43edf |
children | 629876cc0540 |
comparison
equal
deleted
inserted
replaced
330:c8f52ea74803 | 331:e35e15f07c66 |
---|---|
530 | 530 |
531 // (Re)create index | 531 // (Re)create index |
532 // | 532 // |
533 int sidlib_sldb_build_index(SIDLibSLDB *dbh) | 533 int sidlib_sldb_build_index(SIDLibSLDB *dbh) |
534 { | 534 { |
535 if (dbh == NULL) | |
536 return THERR_NULLPTR; | |
537 | |
535 // Free old index | 538 // Free old index |
536 th_free_r(&(dbh->pindex)); | 539 th_free_r(&(dbh->pindex)); |
537 | 540 |
538 // Get size of db | 541 // Get size of db |
539 dbh->nnodes = th_llist_length((th_llist_t *) dbh->nodes); | 542 dbh->nnodes = th_llist_length((th_llist_t *) dbh->nodes); |
541 // Check number of nodes | 544 // Check number of nodes |
542 if (dbh->nnodes > 0) | 545 if (dbh->nnodes > 0) |
543 { | 546 { |
544 SIDLibSLDBNode *node; | 547 SIDLibSLDBNode *node; |
545 size_t i; | 548 size_t i; |
549 | |
550 // Check number of nodes against overflow | |
551 if (dbh->nnodes > UINTPTR_MAX / sizeof(SIDLibSTILNode *)) | |
552 return THERR_BOUNDS; | |
546 | 553 |
547 // Allocate memory for index-table | 554 // Allocate memory for index-table |
548 dbh->pindex = (SIDLibSLDBNode **) th_malloc(sizeof(SIDLibSLDBNode *) * dbh->nnodes); | 555 dbh->pindex = (SIDLibSLDBNode **) th_malloc(sizeof(SIDLibSLDBNode *) * dbh->nnodes); |
549 if (dbh->pindex == NULL) | 556 if (dbh->pindex == NULL) |
550 return THERR_MALLOC; | 557 return THERR_MALLOC; |
1121 if (dbh->nnodes > 0) | 1128 if (dbh->nnodes > 0) |
1122 { | 1129 { |
1123 SIDLibSTILNode *node; | 1130 SIDLibSTILNode *node; |
1124 size_t i; | 1131 size_t i; |
1125 | 1132 |
1126 // XXX TODO Check number of nodes? | 1133 // Check number of nodes against overflow |
1134 if (dbh->nnodes > UINTPTR_MAX / sizeof(SIDLibSTILNode *)) | |
1135 return THERR_BOUNDS; | |
1127 | 1136 |
1128 // Allocate memory for index-table | 1137 // Allocate memory for index-table |
1129 dbh->pindex = (SIDLibSTILNode **) th_malloc(sizeof(SIDLibSTILNode *) * dbh->nnodes); | 1138 dbh->pindex = (SIDLibSTILNode **) th_malloc(sizeof(SIDLibSTILNode *) * dbh->nnodes); |
1130 if (dbh->pindex == NULL) | 1139 if (dbh->pindex == NULL) |
1131 return THERR_MALLOC; | 1140 return THERR_MALLOC; |