GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Avoid unchecked list indexing in JSON encoder · RustPython/RustPython@7ac7328

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • crates/stdlib/src
    • json.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎crates/stdlib/src/json.rs‎

Copy file name to clipboardExpand all lines: crates/stdlib/src/json.rs
+14-5Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ mod _json {
11021102
/// collection on every step (rather than working off a
11031103
/// pre-collected snapshot) so that a `default()`/nested-encoder
11041104
/// callback that shrinks the list mid-encode is observed the same
1105-
/// way CPython's `PySequence_Fast`-free direct indexing observes it
1105+
/// way CPython's live sequence iteration observes it
11061106
/// (see `test_encode_mutated`: deleting the last element on every
11071107
/// callback call causes encoding to stop early once the shrinking
11081108
/// list's length catches up with the read cursor).
@@ -1113,7 +1113,7 @@ mod _json {
11131113
out: &mut Wtf8Buf,
11141114
vm: &VirtualMachine,
11151115
len: impl Fn() -> usize,
1116-
get: impl Fn(usize) -> PyObjectRef,
1116+
get: impl Fn(usize) -> Option<PyObjectRef>,
11171117
) -> PyResult<()> {
11181118
if len() == 0 {
11191119
out.push_str("[]");
@@ -1128,7 +1128,9 @@ mod _json {
11281128
};
11291129
let mut i = 0;
11301130
while i < len() {
1131-
let value = get(i);
1131+
let Some(value) = get(i) else {
1132+
break;
1133+
};
11321134
if i == 0 {
11331135
self.push_newline_indent(out, new_level);
11341136
} else {
@@ -1311,11 +1313,18 @@ mod _json {
13111313
out,
13121314
vm,
13131315
|| list.borrow_vec().len(),
1314-
|i| list.borrow_vec()[i].clone(),
1316+
|i| list.borrow_vec().get(i).cloned(),
13151317
)
13161318
} else if let Some(tuple) = obj.downcast_ref::<PyTuple>() {
13171319
let slice = tuple.as_slice();
1318-
self.encode_list(obj, level, out, vm, || slice.len(), |i| slice[i].clone())
1320+
self.encode_list(
1321+
obj,
1322+
level,
1323+
out,
1324+
vm,
1325+
|| slice.len(),
1326+
|i| slice.get(i).cloned(),
1327+
)
13191328
} else if let Some(dict) = PyAnyDict::from_object(obj) {
13201329
self.encode_dict(obj, dict, level, out, vm)
13211330
} else {

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.