Repository navigation
fix: correct offline benzene bonds and guard corrupted localStorage - #45
Conversation
The offline benzene fallback interleaved carbon and hydrogen atoms in the
atoms array (C0, H6, C1, H7, ...), but the bond list references atoms by
their array index (carbon i -> i, hydrogen i -> i + 6). As a result every
bond was drawn between the wrong atoms whenever the offline fallback path
was hit. Build all carbons first, then all hydrogens, so each atom's array
index matches the index the bond list references.
Also harden loadHistory/loadFavourites: the try/catch only guarded
JSON.parse, so a valid-but-non-array stored value (e.g. {} after tampering
or a schema change) was returned as MoleculeEntry[] and crashed the app at
render when .some/.filter/.map ran on it. Validate the parsed shape and
fall back to an empty array.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WRqVTYbgdzZKc9kB55P8w4
The benzene offline fallback interleaved carbons and hydrogens in the atoms array (C0, H6, C1, H7, ...), so carbon i landed at array index 2*i. The bond list, however, references atoms by array index (carbon i -> i, hydrogen i -> i + 6), which Molecule3DComponent consumes directly. Every benzene bond was therefore drawn between the wrong atoms on the offline path. Push all carbons first, then all hydrogens, so array indices line up with the bonds. Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01WRqVTYbgdzZKc9kB55P8w4
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.Scanned FilesNone |
|
There was a problem hiding this comment.
🟢 Approval recommended
The changes are small, self-contained, and directly address confirmed correctness issues without introducing new external dependencies or behavioral changes on the normal path.
Pull request overview
This PR applies two small correctness fixes in core utilities: it corrects atom indexing in the offline benzene fallback so bonds render between the intended atoms, and it hardens localStorage history/favourites loading against non-array JSON values to prevent runtime crashes.
Changes:
- Fix offline benzene atom ordering so bond indices align with the atoms array indices.
- Validate parsed
localStoragevalues are arrays before returning them fromloadHistory()/loadFavourites().
File summaries
| File | Description |
|---|---|
| src/utils/storage.ts | Ensures parsed localStorage values are arrays (otherwise returns []) to avoid .map/.filter/.some crashes on non-arrays. |
| src/utils/moleculeParser.ts | Reorders benzene atom construction (carbons first, then hydrogens) so bond indices reference the intended atoms in the offline fallback. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
The SonarCloud Code Analysis check is failing its Quality Gate on one condition: "B Security Rating on New Code" (required ≥ A). All other checks (Node.js CI, Dependency Review, Copilot review) are green. I wasn't able to pin down the specific finding from this environment:
Reviewing the diff itself, I don't see a security vulnerability introduced by this PR. The two changes are:
Could you take a look at the finding on the SonarCloud dashboard? If it's the Generated by Claude Code |




Summary
Two small, self-contained correctness fixes found during a cross-repo maintenance review. No behavior change on the normal (online) path.
1. Offline benzene fallback drew bonds between the wrong atoms
createBenzene()insrc/utils/moleculeParser.tsbuilt the atoms array interleaved (C0, H6, C1, H7, …), so carbon i landed at array index2*i. But the bond list references atoms by array index (atomIndex1: i,atomIndex2: i + 6), andMolecule3DComponentconsumes those indices directly (centeredAtoms[bond.atomIndex1]). The result: every bond in the offline benzene fallback was drawn between the wrong atoms (e.g. the C0–C1 bond went from C0 to H6). All the other fallbacks use sequential ids whereid === index, so only benzene was affected.Fix: push all six carbons first, then all six hydrogens, so each atom's array index matches the index the bond list references. Coordinates and bond orders are unchanged.
2. Corrupted
localStoragecould white-screen the apploadHistory()/loadFavourites()insrc/utils/storage.tsonly guardedJSON.parsein theirtry/catch. A value that is valid JSON but not an array (e.g.{}after tampering or a schema change) was returned asMoleculeEntry[], then crashed at render whenuseFavourites/useMoleculeHistorycalled.some/.filter/.mapon it. Now the parsed value's shape is validated and a non-array falls back to[].Testing
tsc --noEmit— passeseslinton both changed files — passes (0 warnings)Notes
The scan also flagged a stale-closure history entry in
App.tsxand a per-hoverconsole.loginAtomTooltip.tsx; both are cosmetic and left out to keep this PR to correctness fixes.🤖 Generated with Claude Code
https://claude.ai/code/session_01WRqVTYbgdzZKc9kB55P8w4
Generated by Claude Code