Skip to content

Commit

Permalink
VarCharArray::new place terminating zero at end
Browse files Browse the repository at this point in the history
  • Loading branch information
pacman82 committed Apr 30, 2021
1 parent ab2be17 commit 655acdb
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 40 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* `&[u8]` now implements `IntoParameter`.
* `Vec<u8>` now implements `IntoParameter`.

* Fix: `VarCharArray::new` is now placing a terminating zero at the end in case of truncation. As statet in the documentation.

## 0.22.0

* Add `Environment::driver_connect` which forwards more functionality from `SqlDriverConnect`. In particular it is now possible (on windows systems) to complete connection strings via the dialog provided by the ODBC driver and driver manager.
Expand Down
5 changes: 3 additions & 2 deletions odbc-api/src/parameter/varchar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,14 @@ impl<const LENGTH: usize> VarCharArray<LENGTH> {
indicator: NULL_DATA,
};

/// Construct from a slice. If value is longer than `LENGTH` it will be truncated.
/// Construct from a slice. If value is longer than `LENGTH` it will be truncated. In that case
/// the last byte will be set to `0`.
pub fn new(text: &[u8]) -> Self {
let indicator = text.len().try_into().unwrap();
let mut buffer = [0u8; LENGTH];
if text.len() > LENGTH {
buffer.copy_from_slice(&text[..LENGTH]);
// *buffer.last_mut().unwrap() = 0;
*buffer.last_mut().unwrap() = 0;
} else {
buffer[..text.len()].copy_from_slice(text);
};
Expand Down
38 changes: 0 additions & 38 deletions odbc-api/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2227,44 +2227,6 @@ fn insert_truncated_value(profile: &Profile) {
}
}

// #[test_case(MSSQL; "Microsoft SQL Server")]
// #[test_case(MARIADB; "Maria DB")]
// #[test_case(SQLITE_3; "SQLite 3")]
// fn insert_truncated_var_char_array(profile: &Profile) {
// let conn = ENV
// .connect_with_connection_string(profile.connection_string)
// .unwrap();
// let table_name = "InsertedTruncatedVarCharArray";

// // Prepare table content
// setup_empty_table(&conn, profile.index_type, table_name, &["VARCHAR(50)"]).unwrap();

// let memory = "Hello, World!INVALID MEMORY\0";
// // Contains hello world.
// let valid = &memory.as_bytes()[..13];
// // Truncated value. Buffer can only hold 'Hello'
// let parameter = VarCharArray::<5>::new(&valid);
// let result = conn.execute(
// &format!("INSERT INTO {} (a) VALUES (?);", table_name),
// &parameter,
// );

// match result {
// Err(e) => {
// // Failing is fine, especially with an error indicating truncation.
// eprintln!("{}", e)
// }
// Ok(None) => {
// // If this was successful we should make sure we did not insert 'INVALID MEMORY' into
// // the database. The better database drivers do not do this, and this could be seen as
// // wrong, but we are only interessted in unsafe behaviour.
// let actual = table_to_string(&conn, table_name, &["a"]);
// assert_eq!("Hello", actual)
// }
// _ => panic!("Unexpected cursor"),
// }
// }

#[test_case(MSSQL; "Microsoft SQL Server")]
#[test_case(MARIADB; "Maria DB")]
#[test_case(SQLITE_3; "SQLite 3")]
Expand Down

0 comments on commit 655acdb

Please sign in to comment.