Skip to content

Buffer passed to SQLSetConnectAttrW used after function return #1594

@bkline

Description

@bkline

Background

According to the ODBC specification and Microsoft's online documentation:

All buffers are allocated and freed by the application. If a buffer is not deferred, it need only exist for the duration of the call to a function.

SQLSetConnectAttrW is not one of the functions included in the table of functions which are allowed to use deferred buffers.

Expected

The implementation of SQLSetConnectAttrW creates its own copy of non-scalar values it will need to use after the function has returned, and the application can free the buffers for those values as soon as SQLSetConnectAttrW has returned.

Observed

An application using Microsoft's ODBC Driver 18 for SQL Server can crash unless it extends the lifetime of any buffers passed to SQLSetConnectAttrW until after the later call to SQLDriverConnectW has returned.

Repro

The script which can be used to reproduce this behavior is contained in the SQLAlchemy issue in which the problem was first reported.

Important

In order to bypass workarounds implemented to avoid this bug, it is necessary to test with a version of SQLAlchemy earlier than 2.0.48 and a version of pyodbc no later than 5.3.0.

Environment

Component Version
OS Linux x86_64
pyodbc 5.3.0
SQLAlchemy 2.0.47
azure-core 1.39.0
azure-identity 1.25.3
Driver msodbcsql18 18.6.1.1-1

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions