From 0d4961936621dfa73adb0948609888b41dea71bf Mon Sep 17 00:00:00 2001 From: kichikuou Date: Sun, 2 Aug 2026 11:58:06 +0900 Subject: [PATCH] Fix unit map storage for 16-bit variables Store unit map attributes as vmvar_t and derive VR/VW transfer sizes from their element type. This prevents buffer overruns after vmvar_t became uint16_t and fixes upward movement in Rance IV. Add scenario coverage for both transfer directions and verify that adjacent variables remain intact. --- src/cmdv.c | 12 ++++++------ test/test.adv | 1 + test/test.hed | 1 + test/testSA.ALD | Bin 8720 -> 9232 bytes test/unitmap.adv | 30 ++++++++++++++++++++++++++++++ 5 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 test/unitmap.adv diff --git a/src/cmdv.c b/src/cmdv.c index 81f7b8b..cff6f4b 100644 --- a/src/cmdv.c +++ b/src/cmdv.c @@ -90,7 +90,7 @@ typedef struct { #define UNITMAP_ATTRIB_WALKRESULT (3) /* UnitMAP 全体へのポインタ */ -static int *UnitMap = NULL; +static vmvar_t *UnitMap = NULL; /* VC command */ static int nPageNum; static int x0Map; @@ -160,7 +160,7 @@ void commandVC() { /* from Rance4 */ return; } - UnitMap = (int *)calloc(cxMap * cyMap * nPageNum * UNITMAP_ATTRIB_DEPTH, sizeof(int)); + UnitMap = calloc(cxMap * cyMap * nPageNum * UNITMAP_ATTRIB_DEPTH, sizeof(*UnitMap)); srcimg = (UnitMapSrcImg *)calloc(nPageNum, sizeof(UnitMapSrcImg)); if (NULL == UnitMap || NULL == srcimg) { @@ -458,7 +458,7 @@ void commandVR() { /* from Rance4 */ int nPage = getCaliValue(); int nType = getCaliValue(); vmvar_t *var = getCaliVariable(); - int *dst; + vmvar_t *dst; TRACE("VR %d,%d,%p:",nPage, nType, var); @@ -481,14 +481,14 @@ void commandVR() { /* from Rance4 */ return; } - memcpy(dst, var, sizeof(int) * MAPSIZE_PER_ATTRIB); + memcpy(dst, var, MAPSIZE_PER_ATTRIB * sizeof(*dst)); } void commandVW() { /* from Rance4 */ int nPage = getCaliValue(); int nType = getCaliValue(); vmvar_t *var = getCaliVariable(); - int *src; + vmvar_t *src; TRACE("VW %d,%d,%p:",nPage, nType, var); @@ -511,7 +511,7 @@ void commandVW() { /* from Rance4 */ return; } - memcpy(var, src, sizeof(int) * MAPSIZE_PER_ATTRIB); + memcpy(var, src, MAPSIZE_PER_ATTRIB * sizeof(*src)); } void commandVE() { /* from T2 */ diff --git a/test/test.adv b/test/test.adv index 811cb0a..e428312 100644 --- a/test/test.adv +++ b/test/test.adv @@ -15,6 +15,7 @@ %#stack.adv: %#math.adv: %#variable.adv: + %#unitmap.adv: HH0,tests_passed: ' tests passed' R HH0,tests_failed: ' tests failed' R diff --git a/test/test.hed b/test/test.hed index 5517c43..3ac2c81 100644 --- a/test/test.hed +++ b/test/test.hed @@ -7,6 +7,7 @@ strvar.adv stack.adv math.adv variable.adv +unitmap.adv #DLLHeader ShString.HEL diff --git a/test/testSA.ALD b/test/testSA.ALD index 10bd02e1d0a9d3e4e21201e89a88ac705266e598..df84e3d746cfd6b607d968327a0d1cee789d7d30 100644 GIT binary patch delta 588 zcmbQ>GQnel9HYua#rqTgNii{Uf*1zulbBez{xbl9!ro?mrOA9uQjGs6Ycg$QQuUa8 zlgWVn8&Jy8;oD>Z=C_krIM^V{HuJIkVuZ8Velf$@T)&v$Y~Ei8^$9eeqsT- zpuU9>=7PJ7rFoepxrqgO#mV^~cQFJTTNo$+X^e=mdn0^*|IBDhf+XxcNPyh->BLp`A1%Y9tI5P$&Hd&E1ev%R=J0~y@fB*oXM6`hb delta 172 zcmbQ>F~Mbm+(hNO6Af4=F-lBSx;N2)YZ4O+*B=H3hKB09e_KQ+^D#*={+X=Fv~luX zCVj!rKnX_&5UJQOtv)J>VX^|}gNX)glUO*|AR0FFvHW6$v)O(z!`WQFnBZ*QUkLSL VNNS{z)X08fnWVzW&H=Ox2mnf4KL!8* diff --git a/test/unitmap.adv b/test/unitmap.adv new file mode 100644 index 0000000..ad8f62e --- /dev/null +++ b/test/unitmap.adv @@ -0,0 +1,30 @@ + MS FILE,"unitmap.adv": + !unitmap_src0:11!!unitmap_src1:22!!unitmap_src2:33!!unitmap_src3:44! + !unitmap_dst0:0!!unitmap_dst1:0!!unitmap_dst2:0!!unitmap_dst3:0!!unitmap_sentinel:1234! + + VC 1,0,0,2,2,1,1: + + ; VR copies consecutive 16-bit VM variables to the unit map. + VR 0,3,unitmap_src0: + VG 0,3,0,0: + ~AssertEquals RND,11,__LINE__: + VG 0,3,1,0: + ~AssertEquals RND,22,__LINE__: + VG 0,3,0,1: + ~AssertEquals RND,33,__LINE__: + VG 0,3,1,1: + ~AssertEquals RND,44,__LINE__: + + ; VW performs the reverse conversion without overwriting adjacent variables. + VS 0,3,0,0,101: + VS 0,3,1,0,102: + VS 0,3,0,1,103: + VS 0,3,1,1,104: + VW 0,3,unitmap_dst0: + ~AssertEquals unitmap_dst0,101,__LINE__: + ~AssertEquals unitmap_dst1,102,__LINE__: + ~AssertEquals unitmap_dst2,103,__LINE__: + ~AssertEquals unitmap_dst3,104,__LINE__: + ~AssertEquals unitmap_sentinel,1234,__LINE__: + + %0: