Skip to content

Commit

Permalink
String empty check in PLtsql_expr_query_mutator (babelfish-for-postgr…
Browse files Browse the repository at this point in the history
…esql#2432)

Add a check that string is not empty before calling `.front()` on it (that is apparently [an UB on empty string](https://en.cppreference.com/w/cpp/string/basic_string/front)).

There are many usages of `.front()` like this in `tsqlIface.cpp`, but AFAICS they always operate on known non-empty strings. Not sure if more non-empty checks before `.front()` calls are necessary there. Such crashes are deterministic with `-O0`, I've run JDBC test suite with `-O0` server and didn't get any more crashes.

### Issues Resolved

babelfish-for-postgresql#2431

### Test Scenarios Covered ###

Without patch the problem is reproducible with [AVG-Aggregate-common-vu-verify](https://github.com/babelfish-for-postgresql/babelfish_extensions/blob/b38e6e2c0261725627d9ac751721e50501af6eef/test/JDBC/input/AVG-Aggregate-common-vu-verify.sql#L65) existing test (and probably some others).

Signed-off-by: Alex Kasko <[email protected]>
  • Loading branch information
staticlibs authored Mar 29, 2024
1 parent bd0e541 commit 6606b11
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion contrib/babelfishpg_tsql/src/tsqlIface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ void PLtsql_expr_query_mutator::add(int antlr_pos, std::string orig_text, std::s
}
}

if ((orig_text.front() == '"') && (orig_text.back() == '"') && (repl_text.front() == '\'') && (repl_text.back() == '\''))
if (!orig_text.empty() && (orig_text.front() == '"') && (orig_text.back() == '"') && !repl_text.empty() && (repl_text.front() == '\'') && (repl_text.back() == '\''))
{
// Do not validate the positions of strings as these are not replaced by their positions
}
Expand Down

0 comments on commit 6606b11

Please sign in to comment.