pganalyze / pg_query.rs

Rust library to parse, deparse and normalize SQL queries using the PostgreSQL query parser
MIT License
126 stars 12 forks source link

Truncate bugfixes #2

Closed seanlinsley closed 2 years ago

seanlinsley commented 2 years ago

This fixes accidental memory corruption leading to segfaults. When truncating a CTE we need to remove its nested target list truncation from the list of possibilities, otherwise we'll end up referencing an object that no longer exists. This PR uses std::mem::replace to return the previous SelectStmt so it can be removed from truncations before the next iteration.

Two other bugfixes:

Review question: are there other truncation types where we could run into memory corruption? I tried subqueries in both SELECT and FROM and couldn't trigger another segfault.

seanlinsley commented 2 years ago

Oh, I forgot to note: the std::mem::replace code is implemented in such a way that it should prevent the same bug from happening for any other statement type inside a CTE, not just selects.