codeant-ai-for-open-source[bot] commented on code in PR #37184:
URL: https://github.com/apache/superset/pull/37184#discussion_r2696463877
##########
superset-frontend/src/pages/SqlLab/LocationContext.tsx:
##########
@@ -40,13 +40,14 @@ export const LocationProvider: FC = ({ children }: {
children: ReactNode }) => {
const permalink = location.pathname.match(/\/p\/\w+/)?.[0].slice(3);
Review Comment:
**Suggestion:** Runtime error risk: calling `.slice(3)` after optional
chaining on the match result can throw if the match is null/undefined; make the
slice call safe by using optional chaining before `.slice` so no method is
invoked on undefined. [null pointer]
<details>
<summary><b>Severity Level:</b> Critical 🚨</summary>
```mdx
- ❌ SQL Lab page fails to mount (LocationProvider crash).
- ❌ Visiting /sqllab or query-parameter URLs triggers crash.
- ⚠️ Virtual dataset banner evaluation disrupted.
- ⚠️ Any consumer of useLocationState impacted during mount.
```
</details>
```suggestion
const permalink = location.pathname.match(/\/p\/\w+/)?.[0]?.slice(3);
```
<details>
<summary><b>Steps of Reproduction ✅ </b></summary>
```mdx
1. Open the frontend and navigate to the SQL Lab page so the
LocationProvider mounts.
LocationProvider is defined in
superset-frontend/src/pages/SqlLab/LocationContext.tsx
(lines 34-56 in the PR hunk).
2. Ensure the browser pathname does NOT contain a permalink matching /p/\w+
(for example,
visit /sqllab or /sqllab?dbid=1&schema=public — pathname is "/sqllab"). The
code that
computes permalink is at line 40: `const permalink =
location.pathname.match(/\/p\/\w+/)?.[0].slice(3);`.
3. When LocationProvider executes that line,
`location.pathname.match(/\/p\/\w+/)` returns
null, the optional chaining `?.[0]` evaluates to undefined, and `.slice(3)`
is invoked on
undefined causing a TypeError at
superset-frontend/src/pages/SqlLab/LocationContext.tsx:40. This throws
during component
render/mount.
4. Observe the component crash (uncaught TypeError) preventing
LocationProvider from
returning a valid Provider and breaking SQL Lab rendering. Replacing the
line with the
suggested safe call (`?.[0]?.slice(3)`) prevents the TypeError by
short-circuiting slice
when no match exists.
```
</details>
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:** superset-frontend/src/pages/SqlLab/LocationContext.tsx
**Line:** 40:40
**Comment:**
*Null Pointer: Runtime error risk: calling `.slice(3)` after optional
chaining on the match result can throw if the match is null/undefined; make the
slice call safe by using optional chaining before `.slice` so no method is
invoked on undefined.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
```
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]