bug(bq_driver): SQLDriverConnectW corrupts/fails to NUL-terminate outConnectionString (breaks Microsoft Excel / iODBC on macOS)

Abierto
#1,719 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
55/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
cpp
Área
backend

Línea de trabajo

Start in google/cloud/odbc/bq_driver/odbc_api.cc at SQLDriverConnectW and compare its output handling with SQLBrowseConnectW. Check the wire buffer helpers in odbc_type_utils.h and the ODBC truncation behavior described in the issue. Done means using separate UTF-8 storage, respecting the wide output buffer limit, and ensuring the result is NUL-terminated; the issue also requests updating the macOS release tarball after a new release tag.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Summary

When using the Google BigQuery ODBC Driver on macOS with iODBC (sizeof(SQLWCHAR) == 4) and Microsoft Excel / Microsoft Query, calling SQLDriverConnectW corrupts outConnectionString (szConnStrOut), causing Excel's Return Data action (inserting query results as a Table or PivotTable) to fail with:

[iODBC][Driver Manager]Data source name not found and no default driver specified. Driver could not be loaded

Root Cause Analysis
1. Latest Published Release (v1.3.1 / ODBCDriverforBigQuery_macos_latest.tar.gz)

In v1.3.1 (currently the latest binary published on GCS bq-driver-releases and GitHub Releases), SQLDriverConnectW in google/cloud/odbc/bq_driver/odbc_api.cc passed reinterpret_cast<SQLCHAR*>(outConnectionString) to SQLDriverConnectInternal() (which wrote 8-bit UTF-8 bytes into the caller's SQLWCHAR* buffer) and then executed:

outConnectionString = ToSqlWChar(utf16_out_conn_str->data());

Because outConnectionString is a pointer passed by value, reassigning the local parameter was a dead store (optimized out under -O3) and never copied the converted wide characters back into the caller's buffer. Consequently:

  • outConnectionString still contained raw 8-bit UTF-8 bytes (e.g., "DSN=my-bq-dsn;").
  • On macOS, iODBC reads outConnectionString as 32-bit wchar_t (UTF-32LE) code units (0x3d4e5344, ...) and converts them into "??????".
  • When clicking Return Data in Microsoft Query on macOS, Microsoft Query.app passes OutConnectionString to Microsoft Excel.app to open the worksheet connection, which immediately fails in iODBC.
2. Remaining Issues on main (google/cloud/odbc/bq_driver/odbc_api.cc:274-300)

Commit d080bdb282 (#1506) replaced the dead pointer assignment with:

WriteWideToWireBuffer(*utf16_out_conn_str, outConnectionString,
                      out_conn_str_len);

However, three issues remain in SQLDriverConnectW on main:

  1. Missing wire NUL terminator: WriteWideToWireBuffer defaults to bool null_terminate = false (odbc_type_utils.h:188), and unlike SQLBrowseConnectW (odbc_api.cc:402), SQLDriverConnectW neither zeros outConnectionString beforehand nor passes /*null_terminate=*/true.
  2. Buffer overflow when outConnectionStringBufferLen truncates the output: Per the ODBC specification, PopulateOutputConnectionString sets *out_conn_str_len to the full untruncated string length even when out_conn_str_buflen is smaller (SQL_SUCCESS_WITH_INFO / 01004). Passing out_conn_str_len directly to WriteWideToWireBuffer without clamping to outConnectionStringBufferLen - 1 writes past the end of outConnectionString, and Utf8ToUtf16((char*)out_conn_str) on line 287 ignores else only because out_conn_str_len > 0.
  3. Aliasing outConnectionString as a temporary UTF-8 buffer: Casting outConnectionString (SQLWCHAR*) directly to SQLCHAR* aliases the caller's output buffer before wide conversion.

Proposed Fix (google/cloud/odbc/bq_driver/odbc_api.cc)

Use a separate temporary std::vector<SQLCHAR> buffer for SQLDriverConnectInternal, clamp the number of wide characters written to outConnectionStringBufferLen - 1, and pass /*null_terminate=*/true to WriteWideToWireBuffer:

  std::vector<SQLCHAR> utf8_out_buf(
      outConnectionString && outConnectionStringBufferLen > 0
          ? static_cast<size_t>(outConnectionStringBufferLen)
          : 0,
      0);
  SQLCHAR* out_conn_str =
      (!utf8_out_buf.empty()) ? utf8_out_buf.data() : nullptr;
  SQLSMALLINT out_conn_str_len = 0;

  rc = google::cloud::odbc_bq_driver::SQLDriverConnectInternal(
      connectionHandle, windowHandle, sqlchar_in_connection_str,
      inConnectionStringLen, out_conn_str, outConnectionStringBufferLen,
      &out_conn_str_len, driverCompletion);

  // Handle Unicode conversion of output parameters.
  if (SQL_SUCCEEDED(rc) && outConnectionString) {
    if (outConnectionStringBufferLen > 0) {
      std::string val(ToCharStr(out_conn_str));
      StatusRecordOr<std::wstring> utf16_out_conn_str = Utf8ToUtf16(val);
      if (!utf16_out_conn_str) {
        return utf16_out_conn_str.GetCalculatedReturnCode();
      }
      size_t const max_chars =
          static_cast<size_t>(outConnectionStringBufferLen - 1);
      size_t const to_copy =
          std::min<size_t>(utf16_out_conn_str->size(), max_chars);
      WriteWideToWireBuffer(*utf16_out_conn_str, outConnectionString, to_copy,
                            /*null_terminate=*/true);
    } else if (outConnectionStringBufferLen == 0) {
      // Buffer length is 0; do not write to outConnectionString.
    }
  }
  if (outConnectionStringLen) *outConnectionStringLen = out_conn_str_len;

(Note: Once a new release tag is cut, please also update the macOS tarball at https://storage.googleapis.com/bq-driver-releases/odbc/ODBCDriverforBigQuery_macos_latest.tar.gz, which currently still serves v1.3.1.)

Lenguaje dominante
C++
Estrellas
5
Forks
8
Merge medio
2 d 14 h
PR fusionados (30 d)
10

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de googleapis/cpp-bigquery-odbc

Todos los issues de googleapis/cpp-bigquery-odbc

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.