summaryrefslogtreecommitdiff
path: root/lib/igt_debugfs.c
diff options
context:
space:
mode:
authorGabriel Krisman Bertazi <krisman@collabora.co.uk>2017-07-27 01:51:28 -0300
committerArkadiusz Hiler <arkadiusz.hiler@intel.com>2017-08-01 11:55:46 +0300
commit0fbe924227529c05712daaac78a028673b4396b2 (patch)
treeb20a080af825c4298cf2c4c7b2ac2ce96e11bbf6 /lib/igt_debugfs.c
parent9e8b8b0d5734371a7b50ea0087412aceb392fa39 (diff)
lib/igt_debugfs: Prevent compiler warning on unchecked printf format
Commit 34a54192e1fb ("lib/igt_debugfs: Add extended helper to format crc to string") introduced the following compiler warning: igt_debugfs.c: In function ‘igt_crc_to_string_extended’: igt_debugfs.c:373:4: warning: format not a string literal, argument types not checked [-Wformat-nonliteral] i == (crc->n_words - 1) ? '\0' : delimiter); This patch addresses the warning while also preventing a possible bad memory access access if n_words is > 64. I have no clue why we care about the padding size (crc_size), but since this was added recently, I'd rather leave it alone. Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.co.uk> Reviewed-by: Arkadiusz Hiler <arkadiusz.hiler@intel.com>
Diffstat (limited to 'lib/igt_debugfs.c')
-rw-r--r--lib/igt_debugfs.c13
1 files changed, 8 insertions, 5 deletions
diff --git a/lib/igt_debugfs.c b/lib/igt_debugfs.c
index 2aa33586..ee1f0f54 100644
--- a/lib/igt_debugfs.c
+++ b/lib/igt_debugfs.c
@@ -351,7 +351,7 @@ bool igt_check_crc_equal(const igt_crc_t *a, const igt_crc_t *b)
* igt_crc_to_string_extended:
* @crc: pipe CRC value to print
* @delimiter: The delimiter to use between crc words
- * @crc_size: the number of bytes to print per crc word (either 4 or 2)
+ * @crc_size: the number of bytes to print per crc word (between 1 and 4)
*
* This function allocates a string and formats @crc into it, depending on
* @delimiter and @crc_size.
@@ -362,16 +362,19 @@ bool igt_check_crc_equal(const igt_crc_t *a, const igt_crc_t *b)
char *igt_crc_to_string_extended(igt_crc_t *crc, char delimiter, int crc_size)
{
int i;
- char *buf = calloc(128, sizeof(char));
- const char *format[2] = { "%08x%c", "%04x%c" };
+ int len = 0;
+ int field_width = 2 * crc_size; /* Two chars per byte. */
+ char *buf = malloc((field_width+1) * crc->n_words * sizeof(char));
if (!buf)
return NULL;
for (i = 0; i < crc->n_words; i++)
- sprintf(buf + strlen(buf), format[crc_size == 2], crc->crc[i],
- i == (crc->n_words - 1) ? '\0' : delimiter);
+ len += sprintf(buf + len, "%0*x%c", field_width,
+ crc->crc[i], delimiter);
+ /* Eat the last delimiter */
+ buf[len - 1] = '\0';
return buf;
}