Skip to content

Commit

Permalink
guardrail buffer sizes
Browse files Browse the repository at this point in the history
Signed-off-by: garyschulte <[email protected]>
  • Loading branch information
garyschulte committed Jul 10, 2024
1 parent ef5e5e3 commit 010fc4e
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions gnark/gnark-jni/gnark-eip-196.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func eip196altbn128G1Add(javaInputBuf, javaOutputBuf, javaErrorBuf *C.char, cInp
outputLen := (*int)(unsafe.Pointer(cOutputLen))

// Convert error C pointers to Go slices
errorBuf := castErrorBufferEIP196(javaErrorBuf, *errorLen)
errorBuf := castErrorBufferEIP196(javaErrorBuf, errorLen)

if (inputLen > 2*EIP196PreallocateForG1) {
// trunc if input too long
Expand Down Expand Up @@ -108,7 +108,7 @@ func eip196altbn128G1Mul(javaInputBuf, javaOutputBuf, javaErrorBuf *C.char, cInp
outputLen := (*int)(unsafe.Pointer(cOutputLen))

// Convert error C pointers to Go slices
errorBuf := castErrorBufferEIP196(javaErrorBuf, *errorLen)
errorBuf := castErrorBufferEIP196(javaErrorBuf, errorLen)

if inputLen < EIP196PreallocateForG1 {
// if we do not have complete input, return 0
Expand Down Expand Up @@ -160,10 +160,10 @@ func eip196altbn128Pairing(javaInputBuf, javaOutputBuf, javaErrorBuf *C.char, cI
outputLen := (*int)(unsafe.Pointer(cOutputLen))

// Convert error C pointers to Go slices
output := castBufferEIP196(javaOutputBuf, *outputLen)
output := castBufferEIP196(javaOutputBuf, outputLen)

// Convert error C pointers to Go slices
errorBuf := castErrorBufferEIP196(javaErrorBuf, *errorLen)
errorBuf := castErrorBufferEIP196(javaErrorBuf, errorLen)

*outputLen = 32

Expand Down Expand Up @@ -312,17 +312,17 @@ func castBufferToSliceEIP196(buf unsafe.Pointer, length int) []byte {
return slice
}

func castBufferEIP196(javaOutputBuf *C.char, length int) []byte {
bufSize := length
if bufSize < EIP196PreallocateForResult {
func castBufferEIP196(javaOutputBuf *C.char, length *int) []byte {
bufSize := *length
if bufSize != EIP196PreallocateForResult {
bufSize = EIP196PreallocateForResult
}
return (*[EIP196PreallocateForResult]byte)(unsafe.Pointer(javaOutputBuf))[:bufSize:bufSize]
}

func castErrorBufferEIP196(javaOutputBuf *C.char, length int) []byte {
bufSize := length
if bufSize < EIP196PreallocateForError {
func castErrorBufferEIP196(javaOutputBuf *C.char, length *int) []byte {
bufSize := *length
if bufSize != EIP196PreallocateForError {
bufSize = EIP196PreallocateForError
}
return (*[EIP196PreallocateForError]byte)(unsafe.Pointer(javaOutputBuf))[:bufSize:bufSize]
Expand Down

0 comments on commit 010fc4e

Please sign in to comment.