Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subsequent setlocale call resets pointer causing memory access violation #276

Open
sgobiraj opened this issue May 26, 2021 · 0 comments
Open

Comments

@sgobiraj
Copy link

Environment

  • nanodbc version:2.13.0
  • DBMS name/version:Microsoft SQL Server 2017 (RTM) - 14.0.1000.169 (X64)
  • ODBC connection string:
  • OS and Compiler:Windows 10, msvc
  • CMake settings:

Actual behavior

In nanodbc::result::result_impl::get_ref_impl<T,type> for SQL_C_DATE, SQL_C_TIME, SQL_C_TIMESTAMP std::setlocale is used incorrectly resulting in a memory access violation.

case SQL_C_DATE:
{
    const date d = *ensure_pdata<date>(column);

....
char* old_lc_time = std::setlocale(LC_TIME, nullptr); <- this returns pointer to current locale
std::setlocale(LC_TIME, ""); <- subsequent call here deletes memory referenced by old_lc_time
....
std::setlocale(LC_TIME, old_lc_time); <- memory access violation due to invalid old_lc_time pointer

Expected behavior

Minimal Working Example

This can be fixed by taking a copy of string pointed to by old_lc_time before subsequent call to setlocale with "". This way it is a copy on the stack.

matejbrumen added a commit to matejbrumen/nanodbc that referenced this issue Nov 14, 2022
- Replace char* with std::string to persist the memory content and avoid memory access violation when restoring old locale
- Second call to setlocale frees the old_lc_time pointer which causes the memory access violation during restore (3rd call)
mloskot pushed a commit that referenced this issue Nov 15, 2022
* #276 Fix memory access violation when parsing datetime types

- Replace char* with std::string to persist the memory content and avoid memory access violation when restoring old locale
- Second call to setlocale frees the old_lc_time pointer which causes the memory access violation during restore (3rd call)

* Fix possibility of dereferencing nullptr when assigning C String to std::string

- C strings can be null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant