[gnome-keyring/nielsdg/fix-snprintf-truncation-warning: 2/2] gkm: Fix some format-truncation warnings




commit 269ae26c81ed716f2552af016acd5e91f17a93fd
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Sat May 21 14:27:27 2022 +0200

    gkm: Fix some format-truncation warnings
    
    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

 pkcs11/gkm/gkm-attributes.c | 10 +++++++---
 1 file 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));


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]