Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Improve format string parsing: adjacent verbs #729

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions ql/lib/semmle/go/StringOps.qll
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,26 @@ module StringOps {
formatDirective.charAt(1) != "%" and
result = this.getArgument((n / 2) + f.getFirstFormattedParameterIndex())
}

/**
* Returns the `tokenIdx`th token of `fmt`, where a token is a string of either 'a's or 'b's,
* and including this token we have seen `charsSoFar` characters of which `asSoFar` were 'a's.
*/
private string statefulParse(int tokenIdx, int charsSoFar, int asSoFar) {
result = getComponent(tokenIdx) and
exists(int asThisToken, int charsPrev, int asPrev |
(if result.charAt(0) = "a" then asThisToken = result.length() else asThisToken = 0) and
(
// Base case:
tokenIdx = 0 and charsPrev = 0 and asPrev = 0
or
// Recursive case:
exists(this.statefulParse(tokenIdx - 1, charsPrev, asPrev))
) and
asSoFar = asPrev + asThisToken and
charsSoFar = charsPrev + result.length()
)
}
}
}

Expand Down