Skip to content

Commit 09018d0

Browse files
committed
Avoid panic in QueryBuilder::reset
1 parent 3a90efc commit 09018d0

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

sqlx-core/src/query_builder.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,9 @@ where
517517
/// The query is truncated to the initial fragment provided to [`new()`][Self::new] and
518518
/// the bind arguments are reset.
519519
pub fn reset(&mut self) -> &mut Self {
520-
let query: &mut String = Arc::get_mut(&mut self.query).expect(ERROR);
520+
// Someone can hold onto a clone of `self.query` by calling build and not executing the
521+
// query. To avoid panicking here we might as well allocate a new `String`.
522+
let query: &mut String = Arc::make_mut(&mut self.query);
521523
query.truncate(self.init_len);
522524
self.arguments = Some(Default::default());
523525

tests/postgres/query_builder.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,10 @@ fn test_reset() {
7979
fn test_query_builder_reuse() {
8080
let mut qb: QueryBuilder<'_, Postgres> = QueryBuilder::new("");
8181

82-
{
83-
let _query = qb
84-
.push("SELECT * FROM users WHERE id = ")
85-
.push_bind(42i32)
86-
.build();
87-
}
82+
let _query = qb
83+
.push("SELECT * FROM users WHERE id = ")
84+
.push_bind(42i32)
85+
.build();
8886

8987
qb.reset();
9088

0 commit comments

Comments
 (0)