Skip to content

Commit

Permalink
Fix bug where env.GetUTF8String() would fail when env.PrecalculateSig…
Browse files Browse the repository at this point in the history
…nature() had been set.

M	jnigi.go
M	jnigi_test.go
  • Loading branch information
timob committed Dec 15, 2023
1 parent 449ebff commit 2fbd8c4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions jnigi.go
Original file line number Diff line number Diff line change
Expand Up @@ -1881,13 +1881,18 @@ var utf8 *ObjectRef
// GetUTF8String return global reference to java/lang/String containing "UTF-8"
func (j *Env) GetUTF8String() *ObjectRef {
if utf8 == nil {
// make sure we don't use any preCalSig set when we do NewObject
savePrecalSig := j.preCalcSig
j.preCalcSig = ""

str, err := j.NewObject("java/lang/String", []byte("UTF-8"))
if err != nil {
panic(err)
}
global := j.NewGlobalRef(str)
j.DeleteLocalRef(str)
utf8 = global
j.preCalcSig = savePrecalSig
}

return utf8
Expand Down
9 changes: 9 additions & 0 deletions jnigi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ func PTestBasic(t *testing.T) {
t.Fatal(err)
}

// test env.GetUTF8String() should be independent of PrecalculateSignature
sigStr := "dummy"
env.PrecalculateSignature(sigStr)
env.GetUTF8String()
if !assert.Equal(t, sigStr, env.preCalcSig) {
t.Fail()
}
env.preCalcSig = ""

// byte array argument, byte array method
var testStr string = "hello world"
str, err := env.NewObject("java/lang/String", []byte(testStr))
Expand Down

0 comments on commit 2fbd8c4

Please sign in to comment.