From 3ee314dde16d1d69ddf840cdb8b5aa186c592262 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Thu, 20 Jul 2017 13:36:44 +0200 Subject: [PATCH] gpg: Make function mk_datestr public. * g10/keydb.h (MK_DATESTR_SIZE): New. * g10/keyid.c (mk_datestr): Make public. Add arg bufsize and use snprintf. Change arg atime to u32. (datestr_from_pk): Simplify. (datestr_from_sig): Ditto. (expirestr_from_pk): Ditto. (expirestr_from_sig): Ditto. (revokestr_from_pk): Ditto. -- Note that this also reduces the size of the static buffers from 16 to 11 which is sufficient for the string. In the past we added the 5 extra bytes to cope for bugs in gmtime which is now handles by snprintf. Signed-off-by: Werner Koch --- g10/keydb.h | 7 +++++++ g10/keyid.c | 40 ++++++++++++++++++---------------------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/g10/keydb.h b/g10/keydb.h index bad314991..803987b8c 100644 --- a/g10/keydb.h +++ b/g10/keydb.h @@ -458,6 +458,13 @@ u32 keyid_from_fingerprint (ctrl_t ctrl, const byte *fprint, size_t fprint_len, u32 *keyid); byte *namehash_from_uid(PKT_user_id *uid); unsigned nbits_from_pk( PKT_public_key *pk ); + +/* Convert an UTC TIMESTAMP into an UTC yyyy-mm-dd string. Return + * that string. The caller should pass a buffer with at least a size + * of MK_DATESTR_SIZE. */ +char *mk_datestr (char *buffer, size_t bufsize, u32 timestamp); +#define MK_DATESTR_SIZE 11 + const char *datestr_from_pk( PKT_public_key *pk ); const char *datestr_from_sig( PKT_signature *sig ); const char *expirestr_from_pk( PKT_public_key *pk ); diff --git a/g10/keyid.c b/g10/keyid.c index 24a56433b..d733156f8 100644 --- a/g10/keyid.c +++ b/g10/keyid.c @@ -613,9 +613,13 @@ nbits_from_pk (PKT_public_key *pk) } -static const char * -mk_datestr (char *buffer, time_t atime) +/* Convert an UTC TIMESTAMP into an UTC yyyy-mm-dd string. Return + * that string. The caller should pass a buffer with at least a size + * of MK_DATESTR_SIZE. */ +char * +mk_datestr (char *buffer, size_t bufsize, u32 timestamp) { + time_t atime = timestamp; struct tm *tp; if (IS_INVALID_TIME_T (atime)) @@ -623,8 +627,8 @@ mk_datestr (char *buffer, time_t atime) else { tp = gmtime (&atime); - sprintf (buffer,"%04d-%02d-%02d", - 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday ); + snprintf (buffer, bufsize, "%04d-%02d-%02d", + 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday ); } return buffer; } @@ -638,59 +642,51 @@ mk_datestr (char *buffer, time_t atime) const char * datestr_from_pk (PKT_public_key *pk) { - static char buffer[11+5]; - time_t atime = pk->timestamp; + static char buffer[MK_DATESTR_SIZE]; - return mk_datestr (buffer, atime); + return mk_datestr (buffer, sizeof buffer, pk->timestamp); } const char * datestr_from_sig (PKT_signature *sig ) { - static char buffer[11+5]; - time_t atime = sig->timestamp; + static char buffer[MK_DATESTR_SIZE]; - return mk_datestr (buffer, atime); + return mk_datestr (buffer, sizeof buffer, sig->timestamp); } const char * expirestr_from_pk (PKT_public_key *pk) { - static char buffer[11+5]; - time_t atime; + static char buffer[MK_DATESTR_SIZE]; if (!pk->expiredate) return _("never "); - atime = pk->expiredate; - return mk_datestr (buffer, atime); + return mk_datestr (buffer, sizeof buffer, pk->expiredate); } const char * expirestr_from_sig (PKT_signature *sig) { - static char buffer[11+5]; - time_t atime; + static char buffer[MK_DATESTR_SIZE]; if (!sig->expiredate) return _("never "); - atime=sig->expiredate; - return mk_datestr (buffer, atime); + return mk_datestr (buffer, sizeof buffer, sig->expiredate); } const char * revokestr_from_pk( PKT_public_key *pk ) { - static char buffer[11+5]; - time_t atime; + static char buffer[MK_DATESTR_SIZE]; if(!pk->revoked.date) return _("never "); - atime=pk->revoked.date; - return mk_datestr (buffer, atime); + return mk_datestr (buffer, sizeof buffer, pk->revoked.date); }