gitdiff-parser icon indicating copy to clipboard operation
gitdiff-parser copied to clipboard

bug: Accessing `String.slice` on an undefined value

Open flacial opened this issue 1 year ago • 0 comments

Expected Behavior

Should parse the diff without any errors or handle the errors gracefully.

Current Behavior

Throws an error when the diff is incorrect

Possible Solution

Check if the string is defined before accessing the .slice method.

Steps to Reproduce

  1. Pass the following diff to gitDiffParser.parse method
'diff --git \nindex 9c96b34..853bddf 100644\n---@@ -1,8 +1,19 @@\n-// write your code here!\n const solution = () => {\n-  // global clear all timeout:\n+  const allT = [];\n+  const old = setTimeout;\n+  window.setTimeout = (func, delay) => {\n+    const realTimeout = old(func, delay);\n+    allT.push(realTimeout);\n+    return realTimeout;\n+  };\n+  window.clearAllTimouts = () => {\n+    while (allT.length) {\n+      clearTimeout(allT.pop());\n+    }\n+  };\n   cat = () => {\n-  }\n+    window.clearAllTimouts();\n+  };\n };\n \n module.exports = solution;'

It will throw:

TypeError: Cannot read properties of undefined (reading 'slice')

at parsePathFromFirstLine (node_modules/gitdiff-parser/index.js:21:35)

Context

It started happening after upgrading our app package to version 0.3.1 and I believe the issue started happening after this commit has been merged. The lines causing the error are highlighted.

flacial avatar Mar 17 '23 15:03 flacial