cell_duplicate_far() fix length issue

This commit is contained in:
nick black 2020-08-03 21:22:39 -04:00 committed by Nick Black
parent 7ac3f68f4e
commit 2656cad07a
2 changed files with 8 additions and 6 deletions

View File

@ -385,9 +385,9 @@ static inline char*
pool_egc_copy(const egcpool* e, const cell* c){ pool_egc_copy(const egcpool* e, const cell* c){
char* ret; char* ret;
if(cell_simple_p(c)){ if(cell_simple_p(c)){
if( (ret = (char*)malloc(2)) ){ if( (ret = (char*)malloc(sizeof(c->gcluster) + 1)) ){
ret[0] = c->gcluster; memset(ret, 0, sizeof(c->gcluster) + 1);
ret[1] = '\0'; memcpy(ret, &c->gcluster, sizeof(c->gcluster));
} }
}else{ }else{
ret = strdup(egcpool_extended_gcluster(e, c)); ret = strdup(egcpool_extended_gcluster(e, c));
@ -599,7 +599,7 @@ cell_duplicate_far(egcpool* tpool, cell* targ, const ncplane* splane, const cell
targ->channels = c->channels; targ->channels = c->channels;
if(cell_simple_p(c)){ if(cell_simple_p(c)){
targ->gcluster = c->gcluster; targ->gcluster = c->gcluster;
return !!c->gcluster; return strnlen((char*)&c->gcluster, sizeof(c->gcluster));
} }
assert(splane); assert(splane);
const char* egc = extended_gcluster(splane, c); const char* egc = extended_gcluster(splane, c);

View File

@ -418,8 +418,10 @@ TEST_CASE("Wide") {
free(egc); free(egc);
cell cl = CELL_TRIVIAL_INITIALIZER, cr = CELL_TRIVIAL_INITIALIZER; cell cl = CELL_TRIVIAL_INITIALIZER, cr = CELL_TRIVIAL_INITIALIZER;
CHECK(3 == ncplane_at_yx_cell(n_, 1, 1, &cl)); CHECK(3 == ncplane_at_yx_cell(n_, 1, 1, &cl));
REQUIRE(cell_extended_gcluster(n_, &cl)); egc = cell_strdup(n_, &cl);
CHECK(0 == strcmp("", extended_gcluster(n_, &cl))); REQUIRE(nullptr != egc);
CHECK(0 == strcmp("", egc));
free(egc);
CHECK(0 == ncplane_at_yx_cell(n_, 1, 2, &cr)); CHECK(0 == ncplane_at_yx_cell(n_, 1, 2, &cr));
REQUIRE(cell_simple_p(&cr)); REQUIRE(cell_simple_p(&cr));
CHECK(0 == cr.gcluster); CHECK(0 == cr.gcluster);