diff options
author | Niels De Graef <nielsdegraef@gmail.com> | 2022-05-21 14:27:27 +0200 |
---|---|---|
committer | Niels De Graef <nielsdegraef@gmail.com> | 2022-05-21 14:35:10 +0200 |
commit | 269ae26c81ed716f2552af016acd5e91f17a93fd (patch) | |
tree | bed0f30fd3ee023ac3e941f1b7cdc73845233560 | |
parent | 5c0a464633631d4ee5fc912f4a5b5bcc2051b8ce (diff) | |
download | gnome-keyring-269ae26c81ed716f2552af016acd5e91f17a93fd.tar.gz |
gkm: Fix some format-truncation warningsnielsdg/fix-snprintf-truncation-warning
When enabling fatal warnings (like in our CI), the
`-Werror=format-truncation=` flag is complaining about us using
`snprintf()` in a way that can theoretically truncate its arguments,
even though this will not happen in practice, unless people are still
running gnome-keyring in the year 100000.
Solve the warning by doing a dumb check that aborts if `snprintf`
returns an error
-rw-r--r-- | pkcs11/gkm/gkm-attributes.c | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/pkcs11/gkm/gkm-attributes.c b/pkcs11/gkm/gkm-attributes.c index dfdd08f3..69320ffb 100644 --- a/pkcs11/gkm/gkm-attributes.c +++ b/pkcs11/gkm/gkm-attributes.c @@ -28,6 +28,7 @@ #include <stdarg.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> @@ -216,15 +217,18 @@ gkm_attribute_set_date (CK_ATTRIBUTE_PTR attr, time_t time) g_return_val_if_reached (CKR_GENERAL_ERROR); g_assert (sizeof (date.year) == 4); - snprintf ((char*)buf, 5, "%04d", 1900 + tm.tm_year); + if (G_UNLIKELY (snprintf ((char*)buf, 5, "%04d", 1900 + tm.tm_year) < 0)) + abort (); memcpy (date.year, buf, 4); g_assert (sizeof (date.month) == 2); - snprintf ((char*)buf, 3, "%02d", tm.tm_mon + 1); + if (G_UNLIKELY (snprintf ((char*)buf, 3, "%02d", tm.tm_mon + 1) < 0)) + abort (); memcpy (date.month, buf, 2); g_assert (sizeof (date.day) == 2); - snprintf ((char*)buf, 3, "%02d", tm.tm_mday); + if (G_UNLIKELY (snprintf ((char*)buf, 3, "%02d", tm.tm_mday) < 0)) + abort (); memcpy (date.day, buf, 2); return gkm_attribute_set_data (attr, &date, sizeof (date)); |