Skip to content

Fixed msvc and clang warnings #794

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions demo/common/overview.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ overview(struct nk_context *ctx)
static const char *weapons[] = {"Fist","Pistol","Shotgun","Plasma","BFG"};

char buffer[64];
size_t sum = 0;
unsigned long sum = 0;

/* default combobox */
nk_layout_row_static(ctx, 25, 200, 1);
Expand Down Expand Up @@ -443,8 +443,8 @@ overview(struct nk_context *ctx)
nk_combo_end(ctx);
}
/* progressbar combobox */
sum = prog_a + prog_b + prog_c + prog_d;
sprintf(buffer, "%lu", sum);
sum = (unsigned long)(prog_a + prog_b + prog_c + prog_d);
snprintf(buffer, sizeof(buffer), "%lu", sum);

Choose a reason for hiding this comment

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

Is it targeted for demos to be also ANSI C89 (ISO C90) compatible?

Copy link
Author

Choose a reason for hiding this comment

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

snprintf is C99. Is there any compiler an existence that does not support C99 though?

There is a definite issue here and in other similar places: %lu defines unsigned long argument of size 4. sizeof(size_t) is 8 on 64- bit systems, so there is an argument size mismatch.

Choose a reason for hiding this comment

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

Apparently the reviewer's guide clarifies that for demos newer language features are not discouraged. But the library shall remain fully ANSI C89 compliant.

C99 introduced the "%zu" format specifier for size_t. I recommend removing the unsigned long casts and using zu.

if (nk_combo_begin_label(ctx, buffer, nk_vec2(200,200))) {
nk_layout_row_dynamic(ctx, 30, 1);
nk_progress(ctx, &prog_a, 100, NK_MODIFIABLE);
Expand All @@ -455,8 +455,8 @@ overview(struct nk_context *ctx)
}

/* checkbox combobox */
sum = (size_t)(check_values[0] + check_values[1] + check_values[2] + check_values[3] + check_values[4]);
sprintf(buffer, "%lu", sum);
sum = (unsigned long)(check_values[0] + check_values[1] + check_values[2] + check_values[3] + check_values[4]);
snprintf(buffer, sizeof(buffer), "%lu", sum);
if (nk_combo_begin_label(ctx, buffer, nk_vec2(200,200))) {
nk_layout_row_dynamic(ctx, 30, 1);
nk_checkbox_label(ctx, weapons[0], &check_values[0]);
Expand All @@ -467,7 +467,7 @@ overview(struct nk_context *ctx)
}

/* complex text combobox */
sprintf(buffer, "%.2f, %.2f, %.2f", position[0], position[1],position[2]);
snprintf(buffer, sizeof(buffer), "%.2f, %.2f, %.2f", position[0], position[1],position[2]);
if (nk_combo_begin_label(ctx, buffer, nk_vec2(200,200))) {
nk_layout_row_dynamic(ctx, 25, 1);
nk_property_float(ctx, "#X:", -1024.0f, &position[0], 1024.0f, 1,0.5f);
Expand All @@ -477,7 +477,7 @@ overview(struct nk_context *ctx)
}

/* chart combobox */
sprintf(buffer, "%.1f", chart_selection);
snprintf(buffer, sizeof(buffer), "%.1f", chart_selection);
if (nk_combo_begin_label(ctx, buffer, nk_vec2(200,250))) {
size_t i = 0;
static const float values[]={26.0f,13.0f,30.0f,15.0f,25.0f,10.0f,20.0f,40.0f, 12.0f, 8.0f, 22.0f, 28.0f, 5.0f};
Expand Down Expand Up @@ -510,7 +510,7 @@ overview(struct nk_context *ctx)
}

/* time combobox */
sprintf(buffer, "%02d:%02d:%02d", sel_time.tm_hour, sel_time.tm_min, sel_time.tm_sec);
snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d", sel_time.tm_hour, sel_time.tm_min, sel_time.tm_sec);
if (nk_combo_begin_label(ctx, buffer, nk_vec2(200,250))) {
time_selected = 1;
nk_layout_row_dynamic(ctx, 25, 1);
Expand All @@ -521,7 +521,7 @@ overview(struct nk_context *ctx)
}

/* date combobox */
sprintf(buffer, "%02d-%02d-%02d", sel_date.tm_mday, sel_date.tm_mon+1, sel_date.tm_year+1900);
snprintf(buffer, sizeof(buffer), "%02d-%02d-%02d", sel_date.tm_mday, sel_date.tm_mon+1, sel_date.tm_year+1900);
if (nk_combo_begin_label(ctx, buffer, nk_vec2(350,400)))
{
int i = 0;
Expand All @@ -547,7 +547,7 @@ overview(struct nk_context *ctx)
} else sel_date.tm_mon--;
}
nk_layout_row_push(ctx, 0.9f);
sprintf(buffer, "%s %d", month[sel_date.tm_mon], year);
snprintf(buffer, sizeof(buffer), "%s %d", month[sel_date.tm_mon], year);
nk_label(ctx, buffer, NK_TEXT_CENTERED);
nk_layout_row_push(ctx, 0.05f);
if (nk_button_symbol(ctx, NK_SYMBOL_TRIANGLE_RIGHT)) {
Expand Down Expand Up @@ -575,7 +575,7 @@ overview(struct nk_context *ctx)
/* days */
if (week_day > 0) nk_spacing(ctx, week_day);
for (i = 1; i <= days; ++i) {
sprintf(buffer, "%d", i);
snprintf(buffer, sizeof(buffer), "%d", i);
if (nk_button_label(ctx, buffer)) {
sel_date.tm_mday = i;
nk_combo_close(ctx);
Expand Down Expand Up @@ -1085,7 +1085,7 @@ overview(struct nk_context *ctx)
char buffer[64];
nk_layout_row_static(ctx, 18, 150, 1);
for (i = 0; i < 64; ++i) {
sprintf(buffer, "0x%02x", i);
snprintf(buffer, sizeof(buffer), "0x%02x", i);
nk_labelf(ctx, NK_TEXT_LEFT, "%s: scrollable region", buffer);
}
nk_group_end(ctx);
Expand All @@ -1095,7 +1095,7 @@ overview(struct nk_context *ctx)
char buffer[64];
nk_layout_row_dynamic(ctx, 25, 2);
for (i = 0; i < 64; ++i) {
sprintf(buffer, "%08d", ((((i%7)*10)^32))+(64+(i%2)*2));
snprintf(buffer, sizeof(buffer), "%08d", ((((i%7)*10)^32))+(64+(i%2)*2));
nk_button_label(ctx, buffer);
}
nk_group_end(ctx);
Expand Down
3 changes: 3 additions & 0 deletions nuklear.h
Copy link

@klei1984 klei1984 Mar 28, 2025

Choose a reason for hiding this comment

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

This file seems to be the script generated output file that should not be changed by hand. The /src/*.* files are the source code of the library.

Original file line number Diff line number Diff line change
Expand Up @@ -25473,6 +25473,7 @@ nk_draw_selectable(struct nk_command_buffer *out,
{
const struct nk_style_item *background;
struct nk_text text;
nk_zero_struct(text);
text.padding = style->padding;

/* select correct colors/images */
Expand Down Expand Up @@ -29944,6 +29945,7 @@ nk_combo_begin_symbol(struct nk_context *ctx, enum nk_symbol_type symbol, struct
const struct nk_style_item *background;
struct nk_color sym_background;
struct nk_color symbol_color;
nk_zero_struct(sym_background);

NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
Expand Down Expand Up @@ -30041,6 +30043,7 @@ nk_combo_begin_symbol_text(struct nk_context *ctx, const char *selected, int len
const struct nk_style_item *background;
struct nk_color symbol_color;
struct nk_text text;
nk_zero_struct(text);

NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
Expand Down