Skip to content

Commit 47c2830

Browse files
fix: Replace unsafe unwrap() calls with proper error handling in inte… (#18418)
fix: Replace unsafe unwrap() calls with proper error handling in interpreter_set.rs This commit fixes potential panic issues by replacing two unsafe unwrap() calls: 1. scalar.as_string().unwrap() - Now returns a proper error if the scalar is not a string 2. c.index(0).unwrap() - Now returns an Internal error if accessing the first row fails These changes improve the robustness of the SET statement interpreter by providing meaningful error messages instead of panicking, which is crucial for production systems.
1 parent 51144fd commit 47c2830

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/query/service/src/interpreters/interpreter_set.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ impl SetInterpreter {
7070
let mut is_globals = vec![];
7171

7272
for (var, scalar) in self.set.idents.iter().zip(scalars.into_iter()) {
73-
let scalar = scalar.as_string().unwrap();
73+
let scalar = scalar.as_string().ok_or_else(|| {
74+
ErrorCode::BadArguments(format!(
75+
"Expected string value for setting '{}', but got {:?}",
76+
var, scalar
77+
))
78+
})?;
7479
let ok = match var.to_lowercase().as_str() {
7580
// To be compatible with some drivers
7681
"sql_mode" | "autocommit" => false,
@@ -216,8 +221,16 @@ impl Interpreter for SetInterpreter {
216221
datablock
217222
.columns()
218223
.iter()
219-
.map(|c| c.index(0).unwrap().to_owned())
220-
.collect()
224+
.map(|c| {
225+
c.index(0)
226+
.ok_or_else(|| {
227+
ErrorCode::Internal(
228+
"Failed to access first row of datablock column".to_string(),
229+
)
230+
})
231+
.map(|s| s.to_owned())
232+
})
233+
.collect::<Result<Vec<_>>>()?
221234
}
222235
};
223236

0 commit comments

Comments
 (0)