GeezerVison is showing github.com — Unify nul errors · RustPython/RustPython@d127606
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: crates/stdlib/src/grp.rs +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -63,7 +63,7 @@ mod grp { | 63 | 63 | fn getgrnam(name: PyUtf8StrRef, vm: &VirtualMachine) -> PyResult<GroupData> { | 64 | 64 | let gr_name = name.as_str(); | 65 | 65 | if gr_name.contains('\0') { | 66 | | - return Err(exceptions::cstring_error(vm)); | | 66 | + return Err(exceptions::nul_char_error(vm)); | 67 | 67 | } | 68 | 68 | let group = host_grp::getgrnam(gr_name).map_err(|err| err.into_pyexception(vm))?; | 69 | 69 | let group = group.ok_or_else(|| { | |
Collapse file Copy file name to clipboardExpand all lines: crates/stdlib/src/multiprocessing.rs +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -359,6 +359,7 @@ mod _multiprocessing { | 359 | 359 | use rustpython_host_env::multiprocessing::{ | 360 | 360 | self as host_multiprocessing, SemError, TryAcquireStatus, WaitStatus, | 361 | 361 | }; | | 362 | + use rustpython_vm::exceptions; | 362 | 363 |
| 363 | 364 | /// Error type for sem_timedwait operations | 364 | 365 | #[cfg(target_vendor = "apple")] | @@ -811,7 +812,7 @@ mod _multiprocessing { | 811 | 812 | let (handle, name) = | 812 | 813 | SemHandle::create(&args.name, value, args.unlink).map_err(|err| { | 813 | 814 | if err == SemError::InvalidInput && args.name.contains('\0') { | 814 | | - vm.new_value_error("embedded null character") | | 815 | + exceptions::nul_char_error(vm) | 815 | 816 | } else { | 816 | 817 | os_error(vm, err) | 817 | 818 | } | @@ -835,7 +836,7 @@ mod _multiprocessing { | 835 | 836 | fn sem_unlink(name: String, vm: &VirtualMachine) -> PyResult<()> { | 836 | 837 | host_multiprocessing::sem_unlink(&name).map_err(|err| { | 837 | 838 | if err == SemError::InvalidInput && name.contains('\0') { | 838 | | - vm.new_value_error("embedded null character") | | 839 | + exceptions::nul_char_error(vm) | 839 | 840 | } else { | 840 | 841 | os_error(vm, err) | 841 | 842 | } | |
Collapse file Copy file name to clipboardExpand all lines: crates/stdlib/src/openssl.rs +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1041,7 +1041,7 @@ mod _ssl { | 1041 | 1041 | fn set_ciphers(&self, cipherlist: PyStrRef, vm: &VirtualMachine) -> PyResult<()> { | 1042 | 1042 | let ciphers: &str = cipherlist.as_ref(); | 1043 | 1043 | if ciphers.contains('\0') { | 1044 | | - return Err(exceptions::cstring_error(vm)); | | 1044 | + return Err(exceptions::nul_char_error(vm)); | 1045 | 1045 | } | 1046 | 1046 | self.builder() | 1047 | 1047 | .set_cipher_list(ciphers) | @@ -1097,12 +1097,12 @@ mod _ssl { | 1097 | 1097 | Either::A(s) => { | 1098 | 1098 | let s: &str = s.as_ref(); | 1099 | 1099 | if s.contains('\0') { | 1100 | | - return Err(exceptions::cstring_error(vm)); | | 1100 | + return Err(exceptions::nul_char_error(vm)); | 1101 | 1101 | } | 1102 | 1102 | s.to_cstring(vm)? | 1103 | 1103 | } | 1104 | 1104 | Either::B(b) => std::ffi::CString::new(b.borrow_buf().to_vec()) | 1105 | | - .map_err(|_| exceptions::cstring_error(vm))?, | | 1105 | + .map_err(|_| exceptions::nul_char_error(vm))?, | 1106 | 1106 | }; | 1107 | 1107 |
| 1108 | 1108 | // Find the NID for the curve name using OBJ_sn2nid | @@ -2038,7 +2038,7 @@ mod _ssl { | 2038 | 2038 | )); | 2039 | 2039 | } | 2040 | 2040 | if hostname_str.contains('\0') { | 2041 | | - return Err(vm.new_type_error("embedded null character")); | | 2041 | + return Err(exceptions::nul_char_type_error(vm)); | 2042 | 2042 | } | 2043 | 2043 | let ip = hostname_str.parse::<core::net::IpAddr>(); | 2044 | 2044 | if ip.is_err() { | |
Collapse file Copy file name to clipboardExpand all lines: crates/stdlib/src/ssl.rs +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -70,6 +70,7 @@ mod _ssl { | 70 | 70 | sync::atomic::{AtomicUsize, Ordering}, | 71 | 71 | time::Duration, | 72 | 72 | }; | | 73 | + use rustpython_vm::exceptions; | 73 | 74 | use std::{ | 74 | 75 | collections::{HashMap, hash_map::DefaultHasher}, | 75 | 76 | io::BufRead, | @@ -392,7 +393,7 @@ mod _ssl { | 392 | 393 | // SNI will not be sent for IP addresses | 393 | 394 |
| 394 | 395 | if hostname.contains('\0') { | 395 | | - return Err(vm.new_type_error("embedded null character")); | | 396 | + return Err(exceptions::nul_char_type_error(vm)); | 396 | 397 | } | 397 | 398 |
| 398 | 399 | if hostname.len() > 253 { | @@ -1869,7 +1870,7 @@ mod _ssl { | 1869 | 1870 |
| 1870 | 1871 | // Check for NULL bytes | 1871 | 1872 | if hostname.contains('\0') { | 1872 | | - return Err(vm.new_type_error("embedded null character")); | | 1873 | + return Err(exceptions::nul_char_error(vm)); | 1873 | 1874 | } | 1874 | 1875 |
| 1875 | 1876 | Some(hostname.to_string()) | |
Collapse file Copy file name to clipboardExpand all lines: crates/vm/src/buffer.rs +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,6 +3,7 @@ use crate::{ | 3 | 3 | builtins::{PyBaseExceptionRef, PyBytesRef, PyTuple, PyTupleRef, PyTypeRef}, | 4 | 4 | common::{static_cell, str::wchar_t}, | 5 | 5 | convert::ToPyObject, | | 6 | + exceptions, | 6 | 7 | function::{ArgBytesLike, ArgIntoBool, ArgIntoFloat}, | 7 | 8 | }; | 8 | 9 |
| @@ -282,7 +283,7 @@ impl FormatCode { | 282 | 283 |
| 283 | 284 | // Check for embedded null character | 284 | 285 | if c == 0 { | 285 | | - return Err("embedded null character".to_owned()); | | 286 | + return Err(exceptions::NulError.to_string()); | 286 | 287 | } | 287 | 288 |
| 288 | 289 | // PEP3118: Handle extended format specifiers | |
Collapse file Copy file name to clipboardExpand all lines: crates/vm/src/exceptions.rs +46-3Lines changed: 46 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,6 +15,7 @@ use crate::{ | 15 | 15 | suggestion::offer_suggestions, | 16 | 16 | types::{Callable, Constructor, Initializer, Representable}, | 17 | 17 | }; | | 18 | +use core::fmt::{self, Display, Formatter}; | 18 | 19 | use crossbeam_utils::atomic::AtomicCell; | 19 | 20 | use itertools::Itertools; | 20 | 21 | #[cfg(feature = "host_env")] | @@ -1188,20 +1189,62 @@ impl serde::Serialize for SerializeException<'_, '_> { | 1188 | 1189 | } | 1189 | 1190 | } | 1190 | 1191 |
| 1191 | | -pub fn cstring_error(vm: &VirtualMachine) -> PyBaseExceptionRef { | | 1192 | +#[derive(Debug)] | | 1193 | +pub struct NulError; | | 1194 | + | | 1195 | +impl Display for NulError { | | 1196 | + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | | 1197 | + write!(f, "embedded null character") | | 1198 | + } | | 1199 | +} | | 1200 | + | | 1201 | +pub fn nul_char_error(vm: &VirtualMachine) -> PyBaseExceptionRef { | 1192 | 1202 | vm.new_value_error("embedded null character") | 1193 | 1203 | } | 1194 | 1204 |
| | 1205 | +pub fn nul_char_type_error(vm: &VirtualMachine) -> PyBaseExceptionRef { | | 1206 | + vm.new_type_error("embedded null character") | | 1207 | +} | | 1208 | + | | 1209 | +pub fn nul_byte_error(vm: &VirtualMachine) -> PyBaseExceptionRef { | | 1210 | + vm.new_value_error("embedded null byte") | | 1211 | +} | | 1212 | + | 1195 | 1213 | impl ToPyException for alloc::ffi::NulError { | 1196 | 1214 | fn to_pyexception(&self, vm: &VirtualMachine) -> PyBaseExceptionRef { | 1197 | | - cstring_error(vm) | | 1215 | + nul_char_error(vm) | | 1216 | + } | | 1217 | +} | | 1218 | + | | 1219 | +impl ToPyException for alloc::ffi::FromVecWithNulError { | | 1220 | + fn to_pyexception(&self, vm: &VirtualMachine) -> PyBaseExceptionRef { | | 1221 | + nul_char_error(vm) | | 1222 | + } | | 1223 | +} | | 1224 | + | | 1225 | +impl ToPyException for core::ffi::FromBytesWithNulError { | | 1226 | + fn to_pyexception(&self, vm: &VirtualMachine) -> PyBaseExceptionRef { | | 1227 | + nul_char_error(vm) | | 1228 | + } | | 1229 | +} | | 1230 | + | | 1231 | +impl ToPyException for NulError { | | 1232 | + fn to_pyexception(&self, vm: &VirtualMachine) -> PyBaseExceptionRef { | | 1233 | + nul_char_error(vm) | 1198 | 1234 | } | 1199 | 1235 | } | 1200 | 1236 |
| 1201 | 1237 | #[cfg(windows)] | 1202 | 1238 | impl<C> ToPyException for widestring::error::ContainsNul<C> { | 1203 | 1239 | fn to_pyexception(&self, vm: &VirtualMachine) -> PyBaseExceptionRef { | 1204 | | - cstring_error(vm) | | 1240 | + nul_char_error(vm) | | 1241 | + } | | 1242 | +} | | 1243 | + | | 1244 | +#[cfg(windows)] | | 1245 | +impl ToPyException for widestring::error::MissingNulTerminator { | | 1246 | + fn to_pyexception(&self, vm: &VirtualMachine) -> PyBaseExceptionRef { | | 1247 | + vm.new_value_error(self.to_string()) | 1205 | 1248 | } | 1206 | 1249 | } | 1207 | 1250 |
| |
Collapse file Copy file name to clipboardExpand all lines: crates/vm/src/function/fspath.rs +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -40,7 +40,7 @@ impl FsPath { | 40 | 40 | if !check_for_nul || memchr::memchr(b'\0', b).is_none() { | 41 | 41 | Ok(()) | 42 | 42 | } else { | 43 | | - Err(crate::exceptions::cstring_error(vm)) | | 43 | + Err(crate::exceptions::nul_char_error(vm)) | 44 | 44 | } | 45 | 45 | }; | 46 | 46 | let match1 = |obj: PyObjectRef| { | |
Collapse file Copy file name to clipboardExpand all lines: crates/vm/src/stdlib/_codecs.rs +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,7 +13,7 @@ mod _codecs { | 13 | 13 | AsObject, PyObjectRef, PyResult, VirtualMachine, | 14 | 14 | builtins::{PyStrRef, PyUtf8StrRef}, | 15 | 15 | codecs, | 16 | | - exceptions::cstring_error, | | 16 | + exceptions::nul_char_error, | 17 | 17 | function::{ArgBytesLike, FuncArgs}, | 18 | 18 | }; | 19 | 19 |
| @@ -30,7 +30,7 @@ mod _codecs { | 30 | 30 | #[pyfunction] | 31 | 31 | fn lookup(encoding: PyUtf8StrRef, vm: &VirtualMachine) -> PyResult { | 32 | 32 | if encoding.as_str().contains('\0') { | 33 | | - return Err(cstring_error(vm)); | | 33 | + return Err(nul_char_error(vm)); | 34 | 34 | } | 35 | 35 | vm.state | 36 | 36 | .codec_registry | @@ -106,15 +106,15 @@ mod _codecs { | 106 | 106 | #[pyfunction] | 107 | 107 | fn lookup_error(name: PyUtf8StrRef, vm: &VirtualMachine) -> PyResult { | 108 | 108 | if name.as_str().contains('\0') { | 109 | | - return Err(cstring_error(vm)); | | 109 | + return Err(nul_char_error(vm)); | 110 | 110 | } | 111 | 111 | vm.state.codec_registry.lookup_error(name.as_str(), vm) | 112 | 112 | } | 113 | 113 |
| 114 | 114 | #[pyfunction] | 115 | 115 | fn _unregister_error(errors: PyUtf8StrRef, vm: &VirtualMachine) -> PyResult<bool> { | 116 | 116 | if errors.as_str().contains('\0') { | 117 | | - return Err(cstring_error(vm)); | | 117 | + return Err(nul_char_error(vm)); | 118 | 118 | } | 119 | 119 | vm.state | 120 | 120 | .codec_registry | |
Collapse file Copy file name to clipboardExpand all lines: crates/vm/src/stdlib/_io.rs +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -132,7 +132,7 @@ mod _io { | 132 | 132 | }, | 133 | 133 | common::wtf8::{Wtf8, Wtf8Buf}, | 134 | 134 | convert::ToPyObject, | 135 | | - exceptions::cstring_error, | | 135 | + exceptions::nul_char_error, | 136 | 136 | function::{ | 137 | 137 | ArgBytesLike, ArgIterable, ArgMemoryBuffer, ArgSize, Either, FsPath, FuncArgs, | 138 | 138 | IntoFuncArgs, OptionalArg, OptionalOption, PySetterValue, | @@ -2855,7 +2855,7 @@ mod _io { | 2855 | 2855 |
| 2856 | 2856 | fn validate_errors(errors: &PyRef<PyUtf8Str>, vm: &VirtualMachine) -> PyResult<()> { | 2857 | 2857 | if errors.as_str().contains('\0') { | 2858 | | - return Err(cstring_error(vm)); | | 2858 | + return Err(nul_char_error(vm)); | 2859 | 2859 | } | 2860 | 2860 | vm.state | 2861 | 2861 | .codec_registry | @@ -2896,7 +2896,7 @@ mod _io { | 2896 | 2896 | }, | 2897 | 2897 | Some(enc) => { | 2898 | 2898 | if enc.as_str().contains('\0') { | 2899 | | - return Err(cstring_error(vm)); | | 2899 | + return Err(nul_char_error(vm)); | 2900 | 2900 | } | 2901 | 2901 | enc | 2902 | 2902 | } | @@ -2915,7 +2915,7 @@ mod _io { | 2915 | 2915 | }, | 2916 | 2916 | }; | 2917 | 2917 | if encoding.as_str().contains('\0') { | 2918 | | - return Err(cstring_error(vm)); | | 2918 | + return Err(nul_char_error(vm)); | 2919 | 2919 | } | 2920 | 2920 | Ok(encoding) | 2921 | 2921 | } | |
Collapse file Copy file name to clipboardExpand all lines: crates/vm/src/stdlib/_winapi.rs +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -235,12 +235,12 @@ mod _winapi { | 235 | 235 | if let Some(ref name) = args.name | 236 | 236 | && name.as_bytes().contains(&0) | 237 | 237 | { | 238 | | - return Err(crate::exceptions::cstring_error(vm)); | | 238 | + return Err(crate::exceptions::nul_char_error(vm)); | 239 | 239 | } | 240 | 240 | if let Some(ref cmd) = args.command_line | 241 | 241 | && cmd.as_bytes().contains(&0) | 242 | 242 | { | 243 | | - return Err(crate::exceptions::cstring_error(vm)); | | 243 | + return Err(crate::exceptions::nul_char_error(vm)); | 244 | 244 | } | 245 | 245 |
| 246 | 246 | let wcstring = |s: PyStrRef| s.as_wtf8().to_wide_cstring(); | |
|