Skip to content

Commit

Permalink
Fix out-of-bounds error in encoding (#21)
Browse files Browse the repository at this point in the history
## Overview

Previously, the generated `encode()` function was panicking for an
`array out-of-bounds access` error. This was because the loops generated
for setting `finalEncoded` did not increment `j`:

<img width="572" alt="image"
src="https://github.com/celestiaorg/protobuf3-solidity/assets/22297592/aada53a4-0b88-4078-9489-51ec3cfd202b">

This PR fixes this. Tested it and we don't encounter that error anymore.


## Checklist

- [x] New and updated code has appropriate documentation
- [x] New and updated code has new and/or updated testing
- [x] Required CI checks are passing
- [x] Visual proof for any user facing features like CLI or
documentation updates
- [x] Linked issues closed with keywords
  • Loading branch information
MaanavKhaitan authored Feb 4, 2024
1 parent 6593a39 commit a2ec9c6
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1122,19 +1122,19 @@ func (g *Generator) generateMessageEncoder(structName string, fields []*descript
b.P("uint64 j = 0;")
b.P(fmt.Sprintf("while (j < encodedInstance.%s[i].key.length) {", fieldName))
b.Indent()
b.P(fmt.Sprintf("encodedInstance.%s__Encoded[index++] = encodedInstance.%s[i].key[j];", fieldName, fieldName))
b.P(fmt.Sprintf("encodedInstance.%s__Encoded[index++] = encodedInstance.%s[i].key[j++];", fieldName, fieldName))
b.Unindent()
b.P("}")
b.P("j = 0;")
b.P(fmt.Sprintf("while (j < encodedInstance.%s[i].length.length) {", fieldName))
b.Indent()
b.P(fmt.Sprintf("encodedInstance.%s__Encoded[index++] = encodedInstance.%s[i].length[j];", fieldName, fieldName))
b.P(fmt.Sprintf("encodedInstance.%s__Encoded[index++] = encodedInstance.%s[i].length[j++];", fieldName, fieldName))
b.Unindent()
b.P("}")
b.P("j = 0;")
b.P(fmt.Sprintf("while (j < encodedInstance.%s[i].nestedInstance.length) {", fieldName))
b.Indent()
b.P(fmt.Sprintf("encodedInstance.%s__Encoded[index++] = encodedInstance.%s[i].nestedInstance[j];", fieldName, fieldName))
b.P(fmt.Sprintf("encodedInstance.%s__Encoded[index++] = encodedInstance.%s[i].nestedInstance[j++];", fieldName, fieldName))
b.Unindent()
b.P("}")
b.Unindent()
Expand Down Expand Up @@ -1301,15 +1301,15 @@ func (g *Generator) generateMessageEncoder(structName string, fields []*descript
b.P("j = 0;")
b.P(fmt.Sprintf("while (j < encodedInstance.%s__Encoded.length) {", fieldName))
b.Indent()
b.P(fmt.Sprintf("finalEncoded[index++] = encodedInstance.%s__Encoded[j];", fieldName))
b.P(fmt.Sprintf("finalEncoded[index++] = encodedInstance.%s__Encoded[j++];", fieldName))
b.Unindent()
b.P("}")
default:
// Non-message type
b.P("j = 0;")
b.P(fmt.Sprintf("while (j < encodedInstance.%s.length) {", fieldName))
b.Indent()
b.P(fmt.Sprintf("finalEncoded[index++] = encodedInstance.%s[j];", fieldName))
b.P(fmt.Sprintf("finalEncoded[index++] = encodedInstance.%s[j++];", fieldName))
b.Unindent()
b.P("}")
}
Expand Down

0 comments on commit a2ec9c6

Please sign in to comment.