Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/ext/extension-functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,16 @@ static void rightFunc(sqlite3_context *context, int argc, sqlite3_value **argv){

z = (char *)sqlite3_value_text(argv[0]);
l = sqlite3_value_int(argv[1]);

if( l<0 ){
/* A negative length means "start before the string": c-l exceeds the
** character count, so the cursor below walks past the terminating nul
** with no bound and eventually faults. Return the empty string that a
** negative length has always produced, without the out-of-bounds walk. */
sqlite3_result_text(context, "", 0, SQLITE_STATIC);
return;
}

zt = z;

while( sqliteCharVal((unsigned char *)zt)!=0){
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/org/sqlite/ExtensionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,32 @@ public void strfilterMatchingWiderCharacter() throws Exception {
rs.close();
}

@Test
@DisabledInNativeImage // assertj Assumptions do not work in native-image tests
public void rightstrNegativeLength() throws Exception {
Utils.assumeJdbcExtensions(conn);

// rightstr computed "characters to skip" as c - l. A negative length made
// that larger than the string, and the cursor walk past the terminating
// nul was unbounded: large magnitudes ran into an unmapped page and killed
// the JVM with SIGSEGV, which no JDBC exception can catch.
ResultSet rs = stat.executeQuery("select rightstr('abcdef', -1000000000)");
assertThat(rs.next()).isTrue();
assertThat(rs.getString(1)).isEmpty();
rs.close();

rs = stat.executeQuery("select rightstr('abcdef', -1)");
assertThat(rs.next()).isTrue();
assertThat(rs.getString(1)).isEmpty();
rs.close();

// positive lengths still select the tail
rs = stat.executeQuery("select rightstr('abcdef', 2)");
assertThat(rs.next()).isTrue();
assertThat(rs.getString(1)).isEqualTo("ef");
rs.close();
}

private void assertReturnsRow(String sql) throws Exception {
try (ResultSet rs = stat.executeQuery(sql)) {
assertThat(rs.next()).isTrue();
Expand Down
Loading