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

fix(c/driver/sqlite): Make SQLite driver C99 compliant #1946

Merged
merged 3 commits into from
Jun 27, 2024

Conversation

WillAyd
Copy link
Contributor

@WillAyd WillAyd commented Jun 27, 2024

closes #1944

In its current state this fails cpplint, which (for arguably good reason) discourages use of gmtime in favor of gmtime_r. The macros are inspired by similar code in the vendored sqlite.c source however, so I think reasonably accommodate that concern.

Not sure if its worth trying to work around that with cpplint or just disable it for C sources

@WillAyd WillAyd requested a review from lidavidm as a code owner June 27, 2024 03:26
@github-actions github-actions bot added this to the ADBC Libraries 13 milestone Jun 27, 2024
'warning_level=2',
'cpp_std=c++17',
]
)

add_project_arguments('-Wno-int-conversion', '-Wno-unused-parameter', language: 'c')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not required for this PR but these are all the warnings showing up in Meson currently. Could tackle these as follow ups to get us to a clean build

@lidavidm
Copy link
Member

You should be able to // NOLINT lines to silence cpplint

if (!key_data) return;
snprintf(key_data, strlen(key), "%s", key);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: why not just memcpy?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will give that a look tomorrow. Tried strcpy originally but cpplint did not appreciate that

#else
if (gmtime_r(&time, &broken_down_time) != &broken_down_time) {
sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MAIN));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, the SQLite docs state that we're not supposed to use this mutex

Copy link
Contributor Author

@WillAyd WillAyd Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch - I just replaced this with the mutex for the sqlite3 connection, which is used elsewhere. That may be a little overkill but I figured easier to implement than a separate static mutex for the time struct

Not an expert in multithreaded C code so definitely open to feedback

c/driver/sqlite/statement_reader.c Outdated Show resolved Hide resolved
@WillAyd WillAyd changed the title Make SQLite driver C99 compliant fix(c/driver/sqlite): Make SQLite driver C99 compliant Jun 27, 2024
#else
if (gmtime_r(&time, &broken_down_time) != &broken_down_time) {
SetError(error, "Could not convert date %" PRId32 " to broken down time", value);
sqlite3_mutex_enter(sqlite3_db_mutex(conn));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the problem with this is if there's multiple connections in the program, this can race on gmtime since there'll be different mutexes... the easiest might be for us to just declare our own global mutex. There's still possibility for races, but well, ideally this is only a fallback and hopefully most platforms have gmtime_r?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least, for CMake/release builds, I'd like to use gmtime_r

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a potentially easier way for now to just explicitly use the feature test macro for the gmtime_r POSIX function in the sqlite code. Hope that standard is old enough by now that most ADBC compilers would be able to provide that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, nice, thanks

@lidavidm lidavidm merged commit 3aa24e3 into apache:main Jun 27, 2024
64 checks passed
@WillAyd WillAyd deleted the c99-standard branch June 28, 2024 14:56
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

Successfully merging this pull request may close these issues.

Make SQLite ADBC C99 compatabile
2 participants