Skip to content

Commit

Permalink
Merge pull request #386 from KAAtheWiseGit/master
Browse files Browse the repository at this point in the history
Fix incorrect string escaping
  • Loading branch information
boyter authored Apr 27, 2023
2 parents 763ed2d + 64d54ff commit 14b9b22
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions examples/issue345/filename.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bool MyFunction() {
func("double\\escaped\\string\\");
// comment
}
23 changes: 21 additions & 2 deletions processor/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,27 @@ func stringState(fileJob *FileJob, index int, endPoint int, stringTrie *Trie, en
return i, currentState
}

// If we are in a literal string we want to ignore the \ check OR we aren't checking for special ones
if ignoreEscape || fileJob.Content[i-1] != '\\' {
is_escaped := false
// if there is an escape symbol before us, investigate
if fileJob.Content[i-1] == '\\' {
num_escapes := 0
for j := i - 1; j > 0; j -= 1 {
if fileJob.Content[j] == '\\' {
num_escapes += 1
} else {
break
}
}

// if number of escapes is even, all escapes are themselves escaped
// otherwise the last escape does escape current string terminator
if num_escapes%2 != 0 {
is_escaped = true
}
}

// If we are in a literal string we want to ignore escapes OR we aren't checking for special ones
if ignoreEscape || !is_escaped {
if checkForMatchSingle(fileJob.Content[i], index, endPoint, endString, fileJob) {
return i, SCode
}
Expand Down
13 changes: 13 additions & 0 deletions test-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,19 @@ do
fi
done

# Issue345 (https://github.com/boyter/scc/issues/345)
a=$(./scc "examples/issue345/" -f csv | sed -n '2 p')
b="C++,4,3,1,0,0,76"
if [ "$a" == "$b" ]; then
echo -e "{GREEN}PASSED string termination check"
else
echo -e "$a"
echo -e "${RED}======================================================="
echo -e "FAILED Should terminate the string properly"
echo -e "=======================================================${NC}"
exit
fi


# Extra case for longer languages that are normally truncated
for i in 'CloudFormation (YAM' 'CloudFormation (JSO'
Expand Down

0 comments on commit 14b9b22

Please sign in to comment.