Skip to content

Commit ddfedb7

Browse files
committed
Fix severe bug in ZGetPropAddr
ZGetPropAddr must return the address of the property data not the size byte. Add other simple operations.
1 parent ab2c028 commit ddfedb7

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

gork/zdictionary_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package gork
22

3-
import (
4-
"fmt"
5-
"testing"
6-
)
3+
import "testing"
74

85
var dictBuf []byte = []byte{
96
3,
@@ -63,7 +60,6 @@ func TestZDictionarySearch(t *testing.T) {
6360
"golang",
6461
}
6562

66-
fmt.Println(dict.words)
6763
for i, w := range dictExpected.words {
6864
pos := dictExpected.entriesPos + uint32(i)*uint32(dictExpected.entrySize)
6965

gork/zobject.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,17 @@ func GetPropertyLen(mem *ZMemory, propertyPos uint32) uint16 {
161161
return uint16(nbytes)
162162
}
163163

164-
func (obj *ZObject) GetFirstPropertyAddr() uint32 {
164+
func (obj *ZObject) GetFirstPropertySizeAddr() uint32 {
165+
// returns the address of the size byte
166+
165167
// text length is in words
166168
textLength := obj.mem.ByteAt(uint32(obj.propertiesPos))
167169
return uint32(obj.propertiesPos) + 1 + uint32(textLength)*2
168170
}
169171

170172
func (obj *ZObject) GetPropertyAddr(propertyId byte) uint32 {
171173
// v3
172-
addr := obj.GetFirstPropertyAddr()
174+
addr := obj.GetFirstPropertySizeAddr()
173175

174176
for {
175177
size := obj.mem.ByteAt(addr)
@@ -182,11 +184,11 @@ func (obj *ZObject) GetPropertyAddr(propertyId byte) uint32 {
182184
return 0
183185
}
184186

187+
// skip size
188+
addr++
185189
if propno == propertyId {
186190
return addr
187191
}
188-
// skip size
189-
addr++
190192

191193
addr += uint32((size >> 5) + 1)
192194
}

gork/zobject_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func TestZObjectGetFirstPropertyAddr(t *testing.T) {
261261
seq.DecodeZString(header)
262262
}
263263

264-
if obj.GetFirstPropertyAddr() != seq.pos {
264+
if obj.GetFirstPropertySizeAddr() != seq.pos {
265265
t.Fail()
266266
}
267267
}
@@ -279,24 +279,28 @@ func TestZObjectGetPropertyAddr(t *testing.T) {
279279
// skip name
280280
seq.DecodeZString(header)
281281
}
282+
282283
propPos := seq.pos
283284

284285
keys := obj.PropertiesIds()
285286

286-
if len(keys) > 0 && obj.GetPropertyAddr(keys[0]) != obj.GetFirstPropertyAddr() {
287+
if len(keys) > 0 && obj.GetPropertyAddr(keys[0]) != obj.GetFirstPropertySizeAddr()+1 {
287288
t.Fail()
288289
}
289290

290291
// v3
291292
keyIdx := 0
292293
for propId := byte(31); propId > 0; propId-- {
293294
expected := uint32(0)
295+
294296
if keyIdx < len(keys) && keys[keyIdx] == propId {
297+
// skip size
298+
propPos++
299+
295300
expected = propPos
301+
296302
keyIdx++
297303

298-
// skip size
299-
propPos++
300304
propPos += uint32(len(zobjectExpected[i].properties[propId]))
301305
}
302306

gork/zoptable.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var oneOpFuncs = []OneOpFunc{
3333
ZGetSibling,
3434
ZGetChild,
3535
ZGetParent,
36-
nil,
36+
ZGetPropLen,
3737
ZInc,
3838
nil,
3939
ZPrintAt,
@@ -52,10 +52,10 @@ var twoOpFuncs = []TwoOpFunc{
5252
nil, // ZJe is a two op func but it accepts VAR count of args
5353
ZJl,
5454
ZJg,
55-
nil,
55+
ZDecChk,
5656
ZIncChk,
5757
ZJin,
58-
nil,
58+
ZTest,
5959
ZOr,
6060
ZAnd,
6161
ZTestAttr,
@@ -66,7 +66,7 @@ var twoOpFuncs = []TwoOpFunc{
6666
ZLoadW,
6767
ZLoadB,
6868
ZGetProp,
69-
nil,
69+
ZGetPropAddr,
7070
nil,
7171
ZAdd,
7272
ZSub,
@@ -300,6 +300,10 @@ func ZJin(zm *ZMachine, childId uint16, parentId uint16) {
300300
zm.Branch(condition)
301301
}
302302

303+
func ZTest(zm *ZMachine, bitmap uint16, flags uint16) {
304+
zm.Branch(bitmap&flags == flags)
305+
}
306+
303307
func ZGetSibling(zm *ZMachine, objectId uint16) {
304308
sibling := zm.objects[objectId-1].sibling
305309
zm.StoreReturn(uint16(sibling))

0 commit comments

Comments
 (0)