Skip to content
Closed
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
12 changes: 6 additions & 6 deletions ext/odbc/php_odbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2217,9 +2217,9 @@ bool odbc_sqlconnect(zval *zv, char *db, char *uid, char *pwd, int cur_opt, bool

/* Force UID and PWD to be set in the DSN */
if (use_uid_arg || use_pwd_arg) {
db_end--;
if ((unsigned char)*(db_end) == ';') {
*db_end = '\0';
size_t base_len = db_len;
if (base_len > 0 && db[base_len - 1] == ';') {
base_len--;
}

char *uid_quoted = NULL, *pwd_quoted = NULL;
Expand All @@ -2235,7 +2235,7 @@ bool odbc_sqlconnect(zval *zv, char *db, char *uid, char *pwd, int cur_opt, bool
}

if (!use_pwd_arg) {
spprintf(&ldb, 0, "%s;UID=%s;", db, uid_quoted);
spprintf(&ldb, 0, "%.*s;UID=%s;", (int) base_len, db, uid_quoted);
}
}

Expand All @@ -2250,12 +2250,12 @@ bool odbc_sqlconnect(zval *zv, char *db, char *uid, char *pwd, int cur_opt, bool
}

if (!use_uid_arg) {
spprintf(&ldb, 0, "%s;PWD=%s;", db, pwd_quoted);
spprintf(&ldb, 0, "%.*s;PWD=%s;", (int) base_len, db, pwd_quoted);
}
}

if (use_uid_arg && use_pwd_arg) {
spprintf(&ldb, 0, "%s;UID=%s;PWD=%s;", db, uid_quoted, pwd_quoted);
spprintf(&ldb, 0, "%.*s;UID=%s;PWD=%s;", (int) base_len, db, uid_quoted, pwd_quoted);
}

if (uid_quoted && should_quote_uid) {
Expand Down
20 changes: 20 additions & 0 deletions ext/odbc/tests/gh_odbc_dsn_immutability.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
odbc_connect() must not mutate the caller's DSN string
--EXTENSIONS--
odbc
--FILE--
<?php
/* The '=' selects the connection-string form and the trailing ';' is what the
* UID/PWD assembly used to overwrite with a NUL, in the caller's own buffer. */
$dsn = 'Driver=DoesNotExist;Database=x;SS001;';
const DSN_CONST = 'Driver=DoesNotExist;Database=x;SS001_CONST;';

@odbc_connect($dsn, 'u', 'p');
@odbc_connect(DSN_CONST, 'u', 'p');

var_dump($dsn);
var_dump(DSN_CONST);
?>
--EXPECT--
string(37) "Driver=DoesNotExist;Database=x;SS001;"
string(43) "Driver=DoesNotExist;Database=x;SS001_CONST;"
Loading