Skip to content

Commit 816f567

Browse files
authored
fix: skip int struct field (#71)
1 parent 140b30a commit 816f567

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

internal/decoder/context.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,31 @@ func skipClassName(buf []byte, cursor int64) (int64, error) {
8080
return end - 2, err
8181
}
8282

83+
// i:{};
84+
// the cursor should point to the beginning `i`
85+
// and it will point to the byte after `;`
86+
func skipInt(buf []byte, cursor int64) (int64, error) {
87+
cursor++
88+
89+
if buf[cursor] != ':' {
90+
return cursor, errors.ErrUnexpected("':' after type operator `i`", cursor, buf[cursor])
91+
}
92+
93+
cursor++
94+
95+
for isInteger[buf[cursor]] {
96+
cursor++
97+
}
98+
99+
if buf[cursor] != ';' {
100+
return cursor, errors.ErrUnexpected("';' after integer", cursor, buf[cursor])
101+
}
102+
103+
cursor++
104+
105+
return cursor, nil
106+
}
107+
83108
func skipString(buf []byte, cursor int64) (int64, error) {
84109
cursor++
85110
sLen, end, err := readLength(buf, cursor)
@@ -141,12 +166,7 @@ func skipValue(buf []byte, cursor, depth int64) (int64, error) {
141166
return skipString(buf, cursor)
142167
// case 'd':
143168
case 'i':
144-
cursor++
145-
end, err := skipLengthWithBothColon(buf, cursor)
146-
if err != nil {
147-
return cursor, err
148-
}
149-
return end + 1, nil
169+
return skipInt(buf, cursor)
150170
case 'b':
151171
cursor++
152172
if buf[cursor] != ':' {

internal/errors/error.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func ErrUnexpectedEnd(msg string, cursor int64) *SyntaxError {
9292
}
9393

9494
func ErrUnexpected(msg string, cursor int64, c byte) *SyntaxError {
95-
return &SyntaxError{msg: fmt.Sprintf("php-serialize: expecting %s, get char '%c' instead", msg, c), Offset: cursor}
95+
return &SyntaxError{msg: fmt.Sprintf("php-serialize: expecting %s, get char '%c' instead at offset: %d", msg, c, cursor), Offset: cursor}
9696
}
9797

9898
func ErrInvalidCharacter(c byte, context string, cursor int64) *SyntaxError {

taskfile.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
version: 3
2+
tasks:
3+
test:
4+
- go test '-coverpkg=github.com/trim21/go-phpserialize/...' -covermode=atomic -coverprofile=coverage.out -count=1 ./...

tests/unmarshal_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,3 +653,22 @@ func TestUnmarshal_error_case(t *testing.T) {
653653
err := phpserialize.Unmarshal([]byte(raw), &tags)
654654
require.NoError(t, err)
655655
}
656+
657+
func TestUnmarshal_issue_69(t *testing.T) {
658+
type UnionInfo struct {
659+
CompanyId int `php:"company_id" json:"company_id"`
660+
CorpID string `php:"corp_id" json:"corp_id"`
661+
OpenID string `php:"openid" json:"openid"`
662+
From string `php:"from" json:"from"`
663+
Uid int `php:"uid" json:"uid"`
664+
UnionID string `php:"unionid" json:"unionid"`
665+
OrgUid int `php:"org_uid,omitempty" json:"org_uid"`
666+
FromType int `php:"from_type" json:"from_type"`
667+
}
668+
669+
ddd := `a:7:{s:4:"from";s:4:"ding";s:6:"openid";s:15:"360946652480232";s:7:"unionid";s:26:"0Cg1QDDRfgyz8qMjqwwN0giEiE";s:10:"company_id";i:1;s:7:"corp_id";s:36:"ding4eb8f5a40a6244aa35c2f4657eb6378f";s:8:"agent_id";i:894146310;s:3:"uid";i:2;}`
670+
671+
unionInfo := UnionInfo{}
672+
673+
require.NoError(t, phpserialize.Unmarshal([]byte(ddd), &unionInfo))
674+
}

0 commit comments

Comments
 (0)