Skip to content

Commit

Permalink
Fix empty dict literal parsing. (#3833)
Browse files Browse the repository at this point in the history
It was errornously parsed as a set literal.
ml86 authored Nov 16, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 01c75fb commit fb2ee30
Showing 3 changed files with 62 additions and 13 deletions.
35 changes: 22 additions & 13 deletions joern-cli/frontends/pysrc2cpg/pythonGrammar.jj
Original file line number Diff line number Diff line change
@@ -3556,25 +3556,34 @@ iexpr setOrDictOrSetCompOrDictComp(): {
ArrayList<iexpr> valueExpressions = new ArrayList<iexpr>();
ArrayList<Comprehension> comprehensions = null;
// If isSet is false it is a dict.
boolean isSet = true;
boolean isSet = false;
} {
<CURLY_OPEN> { startToken = token; }
(
(
(
dictKeyOrSetValueExpr = expression()
| dictKeyOrSetValueExpr = starredBitwiseOr()
| (<DOUBLE_STAR> doubleStarExpr = expression() { isSet = false; })
(
(dictKeyOrSetValueExpr = expression()
| dictKeyOrSetValueExpr = starredBitwiseOr()
)
(
(
<COLON> dictValueExpr = expression()
{
if (doubleStarExpr != null) {
throw new ParseException("TODO");
}
}
)
| (
{
isSet = true;
}
)
)
)
| <DOUBLE_STAR> doubleStarExpr = expression()
)
(
<COLON> dictValueExpr = expression()
{
if (doubleStarExpr != null) {
throw new ParseException("TODO");
}
isSet = false;
}
)?
(
(comprehensions = forIfClauses())
| (
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.joern.pysrc2cpg.cpg

import io.joern.pysrc2cpg.Py2CpgTestContext
import io.shiftleft.codepropertygraph.generated.DispatchTypes
import io.shiftleft.semanticcpg.language.*
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

class DictCpgTests extends AnyWordSpec with Matchers {

"empty dict" should {
lazy val cpg = Py2CpgTestContext.buildCpg("""{}""".stripMargin)

"be represented as `dictLiteral`" in {
val callOption = cpg.call.methodFullName("<operator>.dictLiteral").nextOption()
callOption.isDefined shouldBe true
callOption.get.code shouldBe "{}"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.joern.pysrc2cpg.cpg

import io.joern.pysrc2cpg.Py2CpgTestContext
import io.shiftleft.codepropertygraph.generated.DispatchTypes
import io.shiftleft.semanticcpg.language.*
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

class SetCpgTests extends AnyWordSpec with Matchers {

"set" should {
lazy val cpg = Py2CpgTestContext.buildCpg("""{1}""".stripMargin)

"be represented as `setLiteral`" in {
val callOption = cpg.call.methodFullName("<operator>.setLiteral").nextOption()
callOption.isDefined shouldBe true
callOption.get.code shouldBe "{1}"
}
}
}

0 comments on commit fb2ee30

Please sign in to comment.