Skip to content

Commit

Permalink
Handle maximum table name length (#37)
Browse files Browse the repository at this point in the history
* Handle maximum table name length

* bump patch (#46)

* bump patch

* Update Cargo.toml

* Add a test for checking max. table identifier length

---------

Co-authored-by: Adam Hendel <[email protected]>
  • Loading branch information
vrmiguel and ChuckHend authored Aug 14, 2023
1 parent 977d924 commit e03f09f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pgmq"
version = "0.12.0"
version = "0.12.1"
edition = "2021"
authors = ["Tembo.io"]
description = "Postgres extension for PGMQ"
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pgmq"
version = "0.15.0"
version = "0.15.1"
edition = "2021"
authors = ["Tembo.io"]
description = "A distributed message queue for Rust applications, on Postgres."
Expand Down
27 changes: 26 additions & 1 deletion core/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,23 @@ pub fn pop(name: &str) -> Result<String, PgmqError> {

/// panics if input is invalid. otherwise does nothing.
pub fn check_input(input: &str) -> Result<(), PgmqError> {
let valid = input
// Docs:
// https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

// Default value of `NAMEDATALEN`, set in `src/include/pg_config_manual.h`
const NAMEDATALEN: usize = 64;
// The maximum length of an identifier.
// Longer names can be used in commands, but they'll be truncated
const MAX_IDENTIFIER_LEN: usize = NAMEDATALEN - 1;
// The max length of a PGMQ table, considering its prefix and the underline after it (e.g. "pgmq_")
const MAX_PGMQ_TABLE_LEN: usize = MAX_IDENTIFIER_LEN - TABLE_PREFIX.len() - 1;

let is_short_enough = input.len() <= MAX_PGMQ_TABLE_LEN;
let has_valid_characters = input
.as_bytes()
.iter()
.all(|&c| c.is_ascii_alphanumeric() || c == b'_');
let valid = is_short_enough && has_valid_characters;
match valid {
true => Ok(()),
false => Err(PgmqError::InvalidQueueName {
Expand Down Expand Up @@ -373,6 +386,18 @@ mod tests {
}
}

#[test]
fn check_input_rejects_names_too_large() {
let table_name = "my_valid_table_name";
assert!(check_input(table_name).is_ok());

assert!(check_input(&"a".repeat(58)).is_ok());

assert!(check_input(&"a".repeat(59)).is_err());
assert!(check_input(&"a".repeat(60)).is_err());
assert!(check_input(&"a".repeat(70)).is_err());
}

#[test]
fn test_check_input() {
let invalids = vec!["bad;queue_name", "bad name", "bad--name"];
Expand Down

0 comments on commit e03f09f

Please sign in to comment.