Skip to content

Commit 4c50d85

Browse files
dsymeCopilot
andcommitted
Dispatch fingerprint callbacks only to matching source lines
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent c5320e3 commit 4c50d85

4 files changed

Lines changed: 66 additions & 12 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/fingerprints.test.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,55 @@ test("addFingerprints", async (t) => {
223223
);
224224
});
225225

226+
test("addFingerprints dispatches to every result on the matching line", async (t) => {
227+
await util.withTmpDir(async (tmpDir) => {
228+
const filepath = path.join(tmpDir, "source.c");
229+
fs.writeFileSync(filepath, "first\nsecond\nthird\n");
230+
const hashes = new Map<number, string>();
231+
await fingerprints.hash((line, hash) => hashes.set(line, hash), filepath);
232+
const lines = [1, 2, 2, 3, 999];
233+
const results: sarif.Result[] = lines.map((startLine) => ({
234+
message: { text: "Finding" },
235+
locations: [
236+
{
237+
physicalLocation: {
238+
artifactLocation: { uri: "source.c" },
239+
region: { startLine },
240+
},
241+
},
242+
],
243+
}));
244+
results[0].partialFingerprints = {
245+
primaryLocationLineHash: "existing-fingerprint",
246+
};
247+
const logger = new RecordingLogger();
248+
await fingerprints.addFingerprints(
249+
{
250+
runs: [{ tool: { driver: { name: "test" } }, results }],
251+
},
252+
tmpDir,
253+
logger,
254+
);
255+
256+
t.is(
257+
results[0].partialFingerprints?.primaryLocationLineHash,
258+
"existing-fingerprint",
259+
);
260+
t.true(
261+
logger.hasMessage(
262+
"existing inconsistent fingerprint value existing-fingerprint",
263+
),
264+
);
265+
for (const index of [1, 2, 3]) {
266+
t.is(
267+
results[index].partialFingerprints?.primaryLocationLineHash,
268+
hashes.get(lines[index]),
269+
);
270+
}
271+
t.is(results[4].partialFingerprints, undefined);
272+
});
273+
});
274+
226275
test("missingRegions", async (t) => {
227276
// Run an end-to-end test on a test file
228277
const input = JSON.parse(

‎src/fingerprints.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,9 @@ export async function addFingerprints(
266266
);
267267
// Gather together results for the same file and construct
268268
// callbacks to accept hashes for that file and update the location
269-
const callbacksByFile: { [filename: string]: hashCallback[] } = {};
269+
const callbacksByFile: {
270+
[filename: string]: Map<number, hashCallback[]>;
271+
} = {};
270272
for (const run of sarifLog.runs || []) {
271273
// We may need the list of artifacts to resolve against
272274
const artifacts = run.artifacts || [];
@@ -298,19 +300,20 @@ export async function addFingerprints(
298300
continue;
299301
}
300302
if (!callbacksByFile[filepath]) {
301-
callbacksByFile[filepath] = [];
303+
callbacksByFile[filepath] = new Map();
302304
}
303-
callbacksByFile[filepath].push(
304-
locationUpdateCallback(result, primaryLocation, logger),
305-
);
305+
const startLine = primaryLocation.physicalLocation.region.startLine;
306+
const callbacks = callbacksByFile[filepath].get(startLine) ?? [];
307+
callbacks.push(locationUpdateCallback(result, primaryLocation, logger));
308+
callbacksByFile[filepath].set(startLine, callbacks);
306309
}
307310
}
308311

309312
// Now hash each file that was found
310313
for (const [filepath, callbacks] of Object.entries(callbacksByFile)) {
311-
// A callback that forwards the hash to all other callbacks for that file
314+
// Forward each hash only to results on that line.
312315
const teeCallback = function (lineNumber: number, hashValue: string) {
313-
for (const c of Object.values(callbacks)) {
316+
for (const c of callbacks.get(lineNumber) ?? []) {
314317
c(lineNumber, hashValue);
315318
}
316319
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Improved SARIF fingerprint generation for source files with many results without changing the generated fingerprints.

0 commit comments

Comments
 (0)