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

Some fixes #115

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Some fixes #115

wants to merge 2 commits into from

Conversation

dscho
Copy link

@dscho dscho commented Mar 21, 2025

I am playing around with static analyzers, and the first one complained about these two issues, which are easily fixed.

When accessing an array element (or, in these instances, characters in
a string), the check whether the array index is within bounds should
always come before accessing said element.

Signed-off-by: Johannes Schindelin <[email protected]>
@dscho
Copy link
Author

dscho commented Mar 26, 2025

@pks-t would you kindly have a look?

@@ -851,7 +851,7 @@ void clar__assert_equal(
if (!is_equal) {
if (wcs1 && wcs2) {
int pos;
for (pos = 0; wcs1[pos] == wcs2[pos] && pos < len; ++pos)
for (pos = 0; pos < len && wcs1[pos] == wcs2[pos]; ++pos)
/* find differing byte offset */;
p_snprintf(buf, sizeof(buf), "'%.*ls' != '%.*ls' (at byte %d)",
len, wcs1, len, wcs2, pos);
Copy link
Member

Choose a reason for hiding this comment

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

This is an obvious improvement indeed.

char iso_dt[20];

if (strftime(iso_dt, sizeof(iso_dt), "%Y-%m-%dT%H:%M:%S", tm) == 0)
localtime_r(&timestamp, &tm);
Copy link
Member

Choose a reason for hiding this comment

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

Hm. The change makes sense, but I do wonder whether localtime_r() is even available on all relevant platforms. Our CI indicates that it's not:

D:/a/clar/clar/clar/summary.h:29:9: error: implicit declaration of function 'localtime_r'; did you mean 'localtime_s'? [-Werror=implicit-function-declaration]
   29 |         localtime_r(&timestamp, &tm);
      |         ^~~~~~~~~~~
      |         localtime_s

:(

Copy link
Author

Choose a reason for hiding this comment

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

Good point, Visual C seems to offer localtime_s(), but not localtime_r().

What do you think, should I test for the presence in CMake and then use a conditional #if ... #else ... #endif construct here? Or would you have a different preference?

Copy link
Member

Choose a reason for hiding this comment

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

What do you think, should I test for the presence in CMake and then use a conditional #if ... #else ... #endif construct here? Or would you have a different preference?

@dscho Ideally it'd work without relying on CMake. We do have an #ifdef _WIN32 block in "clar.c". Can we maybe define a compatibility wrapper or something like that over there?

Copy link
Author

Choose a reason for hiding this comment

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

@pks-t fixed. Hopefully the CI run passes.

Copy link
Author

Choose a reason for hiding this comment

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

The `localtime()` function is inherently thread-unsafe and should not be
used anymore. Let's use `localtime_r()` instead.

As the `localtime_r()` function is not available on Windows, let's add a
small shim there that simply calls `localtime_s()`, which is equally
thread-safe.

Helped-by: Patrick Steinhardt <[email protected]>
Signed-off-by: Johannes Schindelin <[email protected]>
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.

2 participants