Skip to content

Commit

Permalink
feat(logic): implement max length on read_string/3 predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Jun 20, 2023
1 parent 9eea54b commit 5c2a834
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions x/logic/predicate/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,23 @@ func ReadString(vm *engine.VM, stream, length, result engine.Term, cont engine.C
default:
return engine.Error(fmt.Errorf("read_string/3: invalid domain for given stream"))
}

var maxLength uint64 = 0
if maxLen, ok := env.Resolve(length).(engine.Integer); ok {
maxLength = uint64(maxLen)
}

var builder strings.Builder
var totalLen uint64 = 0
for {
r, l, err := s.ReadRune()
totalLen += uint64(l)
if err != nil {
if errors.Is(err, io.EOF) {
if err != nil || (maxLength != 0 && totalLen >= maxLength) {
if errors.Is(err, io.EOF) || totalLen >= maxLength {
break
}
return engine.Error(fmt.Errorf("read_string/3: error occurs reading stream: %w", err))
}
totalLen += uint64(l)
_, err = builder.WriteRune(r)
if err != nil {
return engine.Error(fmt.Errorf("read_string/3: failed write string: %w", err))
Expand Down

0 comments on commit 5c2a834

Please sign in to comment.