[p5.js 2.0+ Bug Report]: storeItem() validation branches are missing `return`, so bad input throws a TypeError or is stored anyway
Maintainer thường phản hồi trong vòng 2 ngày
Đánh giá
- Độ khó
- 1/5
- Thời gian dự kiến
- Dưới một giờ
- Mức phù hợp với người mới
- 85/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- javascript
- Lĩnh vực
- data
Hướng nghiên cứu
Bắt đầu trong src/data/local_storage.js: storeItem() quanh dòng 114 và removeItem() quanh dòng 424, nơi ba nhánh kiểm tra báo cáo qua p5._friendlyError rồi sau đó chạy tiếp xuống dưới. Tái hiện bằng đoạn mã trong issue: storeItem(42, 'hello') nên báo cáo và dừng lại thay vì ném TypeError: key.endsWith is not a function, và storeItem('k', undefined) nên để getItem('k') trống thay vì trả về chuỗi "undefined". Hoàn thành nghĩa là mỗi nhánh trả về trước mọi lời gọi localStorage, với các test bao phủ cả hai trường hợp; try/catch được đề xuất quanh hai lời gọi setItem là một bước tiếp theo có thể tách riêng.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- JavaScript
- Star
- 24.1k
- Fork
- 3.9k
- Merge trung bình
- 3 ngày 17 giờ
- Pull request đã merge (30 ngày)
- 33
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của processing/p5.js
-
[p5.js 2.0+ Bug Report]: SVG importer does not respect preserveAspectRatio="none" for <symbol>/<use>Có thể đã có người làm @Danyccsf đã nhận 1 ngày trước. Đang mởArea:Core p5.js 2.0+
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 83/100
processing/p5.js#9257 · 2 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Add unit tests for noiseDetail()Có thể đã có người làm @Pcmhacker-piro đã nhận 3 ngày trước. Đang mởArea:Math Enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
processing/p5.js#9253 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
processing/p5.js#9241 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
[p5.js 2.0+ Bug Report]: Typo in Spanish reference documentation for ellipseMode()Có thể đã có người làm @cgutierrezval đã nhận 6 ngày trước. Đang mởInternationalization p5.js 2.0+
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 95/100
processing/p5.js#9231 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
[p5.js 2.0+ Bug Report]: ReferenceError: p5 is not defined when calling loadPixels/get/copy/mask on p5.MediaElement in ESMCó thể đã có người làm @Pcmhacker-piro đã nhận 11 ngày trước. Đang mởArea:Core Area:DOM
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
processing/p5.js#9189 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
Tất cả issue của processing/p5.js
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
processing/p5.sound.js#123 ·
-
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 82/100
PhilflowIO/dav-mcp#146 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Add a light/dark theme toggleĐang mởgood first issue hacktoberfest
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Tanishq964/trail-kit.#4 ·
-
Request: <brand-name>Đang mởnew icon permissions in review
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
simple-icons/simple-icons#15067 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
status: needs triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
mastra-ai/mastra#26562 · 1 bình luận · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày