Skip to content

gtk.cfg: fix the g_ascii_formatd() configuration - #8954

Open
pduchi wants to merge 1 commit into
cppcheck-opensource:mainfrom
pduchi:gtk-cfg-g-ascii-formatd
Open

pduchi wants to merge 1 commit into
cppcheck-opensource:mainfrom
pduchi:gtk-cfg-g-ascii-formatd

Conversation

@pduchi

@pduchi pduchi commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

I don't have a Trac account, so there is no ticket for this; the details are below. Feel free to create one and I'll add it to the title.

#include <glib.h>

void f(double d)
{
    gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
    g_ascii_formatd(buf, sizeof(buf), "%.2f", d);
    g_print("%s\n", buf);
}

$ cppcheck --library=gtk formatd.c
formatd.c:6:5: warning: g_ascii_formatd format string requires 1 parameter but only 0 are given. [wrongPrintfScanfArgNum]
formatd.c:6:21: warning: Uninitialized variable: buf [uninitvar]

g_ascii_formatd(buffer, buf_len, format, d) formats exactly one double into buffer.

  • The double was configured as arg 4, so the format check did not count it. That entry is removed, and the format check now also checks its type (%d with a double is reported).
  • buffer (arg 1, direction out) was marked not-uninit and strz although it is only written. It now has minsize buf_len instead, which also catches a buffer smaller than buf_len.

Tests: test/cfg/gtk.c covers the valid call, a wrong conversion and a too-small buffer. Both false positives show without the change; cfg/gtk.cfg validates against cppcheck-cfg.rng.

g_ascii_formatd(buffer, buf_len, format, d) formats exactly one double
into buffer, but every call was reported as
  g_ascii_formatd format string requires 1 parameter but only 0 are given.
  Uninitialized variable: buffer
- The double is a fixed argument: with it configured as arg 4 the format
  check did not count it. Leave it to the format string check, which now
  also checks its type.
- buffer is written, not read: it need not be initialized or a string.
  Its size must be at least buf_len.
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.

1 participant