@@ -600,48 +600,30 @@ private boolean isOctalDigit(char ch) {
600
600
return '0' <= ch && ch <= '7' ;
601
601
}
602
602
603
- private String getStringConcatResult (Expression exp ) {
603
+ private Pair < String , OffsetTranslation > getStringConcatResult (Expression exp ) {
604
604
if (exp instanceof BinaryExpression ) {
605
605
BinaryExpression be = (BinaryExpression ) exp ;
606
606
if (be .getOperator ().equals ("+" )) {
607
- String left = getStringConcatResult (be .getLeft ());
608
- String right = getStringConcatResult (be .getRight ());
607
+ Pair < String , OffsetTranslation > left = getStringConcatResult (be .getLeft ());
608
+ Pair < String , OffsetTranslation > right = getStringConcatResult (be .getRight ());
609
609
if (left != null && right != null ) {
610
- return left + right ;
610
+ String str = left .fst () + right .fst ();
611
+
612
+ int delta = be .getRight ().getLoc ().getStart ().getOffset () - be .getLeft ().getLoc ().getStart ().getOffset ();
613
+ int offset = left .fst ().length ();
614
+ return Pair .make (str , left .snd ().append (right .snd (), offset , delta ));
611
615
}
612
616
}
613
617
} else if (exp instanceof Literal ) {
614
618
Literal lit = (Literal ) exp ;
615
619
if (!lit .isStringLiteral ()) {
616
620
return null ;
617
621
}
618
- return lit .getStringValue ();
622
+ return Pair . make ( lit .getStringValue (), makeStringLiteralOffsets ( lit . getRaw ()) );
619
623
}
620
624
return null ;
621
625
}
622
626
623
- private OffsetTranslation computeStringConcatOffset (Expression exp ) {
624
- if (exp instanceof Literal && ((Literal )exp ).isStringLiteral ()) {
625
- String raw = ((Literal ) exp ).getRaw ();
626
- return makeStringLiteralOffsets (raw );
627
- }
628
-
629
- if (exp instanceof BinaryExpression ) {
630
- BinaryExpression be = (BinaryExpression ) exp ;
631
- OffsetTranslation left = computeStringConcatOffset (be .getLeft ());
632
- OffsetTranslation right = computeStringConcatOffset (be .getRight ());
633
-
634
- if (left == null || right == null ) {
635
- return null ;
636
- }
637
- int delta = be .getRight ().getLoc ().getStart ().getOffset () - be .getLeft ().getLoc ().getStart ().getOffset ();
638
- int offset = getStringConcatResult (be .getLeft ()).length ();
639
- return left .append (right , offset , delta );
640
- }
641
-
642
- return null ;
643
- }
644
-
645
627
/**
646
628
* Builds a translation from offsets in a string value back to its original raw literal text
647
629
* (including quotes).
@@ -848,14 +830,15 @@ public Label visit(BinaryExpression nd, Context c) {
848
830
if (extractedAsRegexp .contains (nd )) {
849
831
return key ;
850
832
}
851
- String rawString = getStringConcatResult (nd );
852
- if (rawString == null ) {
833
+ Pair < String , OffsetTranslation > concatResult = getStringConcatResult (nd );
834
+ if (concatResult == null ) {
853
835
return key ;
854
836
}
837
+ String rawString = concatResult .fst ();
855
838
if (rawString .length () > 1000 && !rawString .trim ().isEmpty ()) {
856
839
return key ;
857
840
}
858
- OffsetTranslation offsets = computeStringConcatOffset ( nd );
841
+ OffsetTranslation offsets = concatResult . snd ( );
859
842
Position start = nd .getLoc ().getStart ();
860
843
com .semmle .util .locations .Position startPos = new com .semmle .util .locations .Position (start .getLine (), start .getColumn (), start .getOffset ());
861
844
SourceMap sourceMap = SourceMap .legacyWithStartPos (SourceMap .fromString (nd .getLoc ().getSource ()).offsetBy (0 , offsets ), startPos );
0 commit comments