Skip to content

Commit ab66cb4

Browse files
authored
Fix UTF-16 string length calculation (sciter-sdk#272)
* fix: Utf16FromString includes the trailing zero.
1 parent 23cf373 commit ab66cb4

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

sciter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ func (s *Sciter) Eval(script string) (retval *Value, ok bool) {
400400
if err != nil {
401401
return nil, false
402402
}
403-
cscriptLength := C.UINT(len(u16))
403+
cscriptLength := C.UINT(len(u16) - 1)
404404
cscript := C.LPCWSTR(unsafe.Pointer(&u16[0]))
405405
cretval := (*C.SCITER_VALUE)(unsafe.Pointer(retval))
406406
// cgo call
@@ -827,7 +827,7 @@ func (e *Element) SetText(text string) error {
827827
if err != nil {
828828
return err
829829
}
830-
clength := C.UINT(len(u16))
830+
clength := C.UINT(len(u16) - 1)
831831
ctext := C.LPCWSTR(unsafe.Pointer(&u16[0]))
832832
// cgo call
833833
r := C.SciterSetElementText(e.handle, ctext, clength)
@@ -2329,7 +2329,7 @@ func (pdst *Value) ConvertFromString(str string, how ValueStringConvertType) err
23292329
if err != nil {
23302330
return err
23312331
}
2332-
clen := C.UINT(len(u16))
2332+
clen := C.UINT(len(u16) - 1)
23332333
cstr := C.LPCWSTR(unsafe.Pointer(&u16[0]))
23342334
chow := C.UINT(how)
23352335
// cgo call

utils.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func StringToBytePtr(s string) *byte {
4545
return &bs[0]
4646
}
4747

48+
// returns a UTF-16 string, including the trailing zero
4849
func Utf16FromString(s string) ([]uint16, error) {
4950
for i := 0; i < len(s); i++ {
5051
if s[i] == 0 {

0 commit comments

Comments
 (0)