Skip to content

Commit 92e8f05

Browse files
committed
JS: Avoid emitting column zero in yaml files
1 parent a887ff4 commit 92e8f05

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

javascript/extractor/src/com/semmle/js/extractor/YAMLExtractor.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.semmle.util.data.StringUtil;
44
import com.semmle.util.exception.CatastrophicError;
55
import com.semmle.util.exception.UserError;
6+
import com.semmle.util.locations.LineTable;
67
import com.semmle.util.trap.TrapWriter;
78
import com.semmle.util.trap.TrapWriter.Label;
89
import com.semmle.util.trap.TrapWriter.Table;
@@ -76,8 +77,10 @@ private static enum NodeKind {
7677

7778
private final boolean tolerateParseErrors;
7879

80+
private TextualExtractor textualExtractor;
7981
private LocationManager locationManager;
8082
private TrapWriter trapWriter;
83+
private LineTable lineTable;
8184

8285
/**
8386
* The underlying SnakeYAML parser; we use the relatively low-level {@linkplain Parser} instead of
@@ -93,8 +96,16 @@ public YAMLExtractor(ExtractorConfig config) {
9396
this.tolerateParseErrors = config.isTolerateParseErrors();
9497
}
9598

99+
private LineTable getLineTable() {
100+
if (lineTable == null) {
101+
lineTable = new LineTable(this.textualExtractor.getSource());
102+
}
103+
return lineTable;
104+
}
105+
96106
@Override
97107
public LoCInfo extract(TextualExtractor textualExtractor) {
108+
this.textualExtractor = textualExtractor;
98109
locationManager = textualExtractor.getLocationManager();
99110
trapWriter = textualExtractor.getTrapwriter();
100111

@@ -253,6 +264,18 @@ private void extractLocation(Label label, Mark startMark, Mark endMark) {
253264
endLine = endMark.getLine() + 1;
254265
endColumn = endMark.getColumn();
255266

267+
// Avoid emitting column zero for non-empty locations
268+
if (endColumn == 0 && !(startLine == endLine && startColumn == endColumn)) {
269+
String source = textualExtractor.getSource();
270+
int offset = getLineTable().getOffsetFromPoint(endMark.getLine(), endMark.getColumn()) - 1;
271+
while (offset > 0 && isNewLine((int)source.charAt(offset))) {
272+
--offset;
273+
}
274+
com.semmle.util.locations.Position adjustedEndPos = getLineTable().getEndPositionFromOffset(offset);
275+
endLine = adjustedEndPos.getLine();
276+
endColumn = adjustedEndPos.getColumn();
277+
}
278+
256279
locationManager.emitSnippetLocation(label, startLine, startColumn, endLine, endColumn);
257280
}
258281
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
| .github/workflows/comment_issue.yml:7:12:10:0 | \| | Potential injection from the github.event.comment.body context, which may be controlled by an external user. |
1+
| .github/workflows/comment_issue.yml:7:12:8:48 | \| | Potential injection from the github.event.comment.body context, which may be controlled by an external user. |
22
| .github/workflows/comment_issue.yml:13:12:14:47 | \| | Potential injection from the github.event.comment.body context, which may be controlled by an external user. |
3-
| .github/workflows/comment_issue_newline.yml:9:12:11:0 | \| | Potential injection from the github.event.comment.body context, which may be controlled by an external user. |
3+
| .github/workflows/comment_issue_newline.yml:9:14:10:50 | \| | Potential injection from the github.event.comment.body context, which may be controlled by an external user. |

0 commit comments

Comments
 (0)