[p5.js 2.0+ Bug Report]: storeItem() validation branches are missing `return`, so bad input throws a TypeError or is stored anyway
I maintainer di solito rispondono entro 2 giorni
Valutazione
- Difficoltà
- 1/5
- Tempo stimato
- Meno di un'ora
- Idoneità per principianti
- 85/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- javascript
- Ambito
- data
Direzione di ricerca
Inizia in src/data/local_storage.js: storeItem() intorno alla riga 114 e removeItem() intorno alla riga 424, dove i tre rami di validazione segnalano tramite p5._friendlyError e poi proseguono. Riproduci con lo snippet nell'issue: storeItem(42, 'hello') dovrebbe segnalare e fermarsi invece di lanciare TypeError: key.endsWith is not a function, e storeItem('k', undefined) dovrebbe lasciare getItem('k') vuoto invece di restituire la stringa "undefined". Fatto significa che ogni ramo ritorna prima di qualsiasi chiamata a localStorage, con test che coprono entrambi i casi; il try/catch proposto attorno alle due chiamate setItem è un seguito separabile.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Most appropriate sub-area of p5.js?
- Data
p5.js version
2.3.2 (verified on main @ 94fb07d)
Web browser and version
All
Operating system
All
Steps to reproduce this
Steps:
- Run the sketch below.
storeItem(42, 'hello')prints the friendly error about the key not being a string, then throwsTypeError: key.endsWith is not a functionand halts the sketch.- Comment that line out.
storeItem('k', undefined)prints "You cannot store undefined variables using storeItem()" — and then stores it anyway. getItem('k')returns the string"undefined".
Snippet:
function setup() {
storeItem(42, 'hello'); // friendly error, then an uncaught TypeError
storeItem('k', undefined); // "You cannot store undefined variables..."
print(getItem('k')); // "undefined"
print(typeof getItem('k')); // "string"
}
Cause
None of the three validation branches in storeItem() returns after reporting the error, so execution falls through into code that assumed the input was already valid:
// src/data/local_storage.js:114
fn.storeItem = function (key, value) {
if (typeof key !== 'string') {
p5._friendlyError(
`The argument that you passed to storeItem() - ${key} is not a string.`,
'storeItem'
);
} // <- no return
if (key.endsWith('p5TypeID')) { // <- throws when key is a number
p5._friendlyError(...);
} // <- no return
if (typeof value === 'undefined') {
p5._friendlyError(
'You cannot store undefined variables using storeItem().',
'storeItem'
);
} // <- no return
let type = typeof value;
switch (type) { /* ... 'undefined' hits `default`, unchanged ... */ }
localStorage.setItem(key, value); // stores "undefined"
localStorage.setItem(`${key}p5TypeID`, type); // stores type "undefined"
};
So the non-string-key case produces a friendly error immediately followed by a hard TypeError from the next check, and the undefined-value case produces a message that is simply not true about what happened — the value is written to localStorage, tagged with type "undefined", and handed back by getItem() as a string.
fn.removeItem (src/data/local_storage.js:424) has the same shape: it reports a non-string key and then calls localStorage.removeItem(key) regardless.
Expected behaviour
Reporting an invalid argument should stop the operation. Nothing should be written, nothing downstream should see input it cannot handle, and the message should describe what actually happened.
Suggested fix
Add return; to each validation branch in storeItem() and removeItem():
if (typeof key !== 'string') {
p5._friendlyError(
`The argument that you passed to storeItem() - ${key} is not a string.`,
'storeItem'
);
+ return;
}
…and likewise for the p5TypeID suffix check and the undefined value check.
One related thing, if you want it in the same PR
The two localStorage.setItem calls are unguarded. They throw QuotaExceededError when storage is full and SecurityError where site data is blocked (Safari private browsing, or a sketch embedded in a third-party iframe with cookies restricted) — both of which surface as uncaught exceptions that stop the sketch rather than as friendly errors.
There is also a consistency hazard: if the first setItem succeeds and the second one fails, the value is stored without its p5TypeID tag, and getItem() then returns it as an untyped raw string. Wrapping both calls in a single try/catch that reports through p5._friendlyError would make that legible and keep the pair atomic. Happy to split this out into its own issue if you'd rather keep them separate.
I'd be glad to open a PR for the missing returns with tests for both cases.
- Lingua principale
- JavaScript
- Stelle
- 24.1k
- Fork
- 3.9k
- Merge medio
- 3g 10h
- PR unite (30g)
- 30
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di processing/p5.js
-
[p5.js 2.0+ Bug Report]: SVG importer does not respect preserveAspectRatio="none" for <symbol>/<use>Forse già presa @Danyccsf l’ha presa 4 giorni fa. ApertaArea:Core p5.js 2.0+
Difficoltà 2/5 1-3 ore Idoneità per principianti 83/100
processing/p5.js#9257 · 2 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 2 giorni
-
Add unit tests for noiseDetail()Forse già presa @Pcmhacker-piro l’ha presa 5 giorni fa. ApertaArea:Math Enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
processing/p5.js#9253 ·
I maintainer di solito rispondono entro 2 giorni
-
[p5.js 2.0+ Bug Report]: getURLParams() mis-parses valueless parameters and never percent-decodes valuesForse già presa @mayanksingh-7974 l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
processing/p5.js#9241 · 1 commento ·
I maintainer di solito rispondono entro 2 giorni
-
[p5.js 2.0+ Bug Report]: Typo in Spanish reference documentation for ellipseMode()Forse di nuovo libera @cgutierrezval l’ha presa 9 giorni fa e non c’è nessuna pull request aperta. ApertaInternationalization p5.js 2.0+
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 95/100
processing/p5.js#9231 · 3 commenti ·
I maintainer di solito rispondono entro 2 giorni
-
[p5.js 2.0+ Bug Report]: ReferenceError: p5 is not defined when calling loadPixels/get/copy/mask on p5.MediaElement in ESMForse già presa @Pcmhacker-piro l’ha presa 14 giorni fa. ApertaArea:Core Area:DOM
Difficoltà 2/5 1-3 ore Idoneità per principianti 86/100
processing/p5.js#9189 · 1 commento ·
I maintainer di solito rispondono entro 2 giorni
Tutte le issue di processing/p5.js
Issue simili
-
[Bug]: [A11y] Missing accessible name on icon-only standalone links (WCAG 2.1 SC 2.4.4, 4.1.2)Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
jaegertracing/jaeger-ui#4596 ·
I maintainer di solito rispondono entro 1 giorno
-
Add: CNC Region Calima [CO]Apertachannels:add check:passed
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 72/100
I maintainer di solito rispondono entro 9 giorni
-
module: core
Difficoltà 2/5 1-3 ore Idoneità per principianti 77/100
bigbluebutton/bigbluebutton#26028 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 91/100
twbs/bootstrap#43050 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
status:untriaged
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 78/100
midnightntwrk/servicedesk#248 ·
I maintainer di solito rispondono entro 1 giorno