[X2Go-Commits] [nx-libs] 02/03: nxcomp/src/Timestamp.{cpp, h}: use ::ctime_s or ::ctime_r instead of plain ctime, on-stack buffers and return std::string objects.

git-admin at x2go.org git-admin at x2go.org
Tue Jan 9 01:09:28 CET 2018


This is an automated email from the git hooks/post-receive script.

x2go pushed a commit to branch 3.6.x
in repository nx-libs.

commit 2eb2f2e6ca13d84113e30041ade2dbfa9f4e1432
Author: Mihai Moldovan <ionic at ionic.de>
Date:   Sat Dec 30 09:30:03 2017 +0100

    nxcomp/src/Timestamp.{cpp,h}: use ::ctime_s or ::ctime_r instead of plain ctime, on-stack buffers and return std::string objects.
    
    Fixes: ArcticaProject/nx-libs#616
---
 nxcomp/src/Timestamp.cpp | 55 +++++++++++++++++++++++++++++++++---------------
 nxcomp/src/Timestamp.h   | 16 +++++++++-----
 2 files changed, 49 insertions(+), 22 deletions(-)

diff --git a/nxcomp/src/Timestamp.cpp b/nxcomp/src/Timestamp.cpp
index e7e0c49..4a9dd33 100644
--- a/nxcomp/src/Timestamp.cpp
+++ b/nxcomp/src/Timestamp.cpp
@@ -44,34 +44,55 @@
 
 T_timestamp timestamp;
 
-//
-// The following functions all use the ctime
-// static buffer from the C library.
-//
-
-char *strTimestamp(const T_timestamp &ts)
+std::string strTimestamp(const T_timestamp &ts)
 {
-  char *ctime_now = ctime((time_t *) &ts.tv_sec);
+  std::string ret;
+
+  char ctime_now[26] = { };
+  bool err = true;
 
-  ctime_now[24] = '\0';
+#if HAVE_CTIME_S
+  errno_t retval = ::ctime_s(ctime_now, sizeof(ctime_now), static_cast<const time_t*>(&ts.tv_sec));
 
-  return ctime_now;
+  if (retval != 0)
+#else
+  char *retval = ::ctime_r(static_cast<const time_t*>(&ts.tv_sec), ctime_now);
+
+  if (!(retval))
+#endif
+  {
+    std::cerr << "WARNING: converting time to string failed." << std::endl;
+  }
+  else
+  {
+    /* Replace newline at position 25 with a NULL byte. */
+    ctime_now[24] = '\0';
+
+    ret = ctime_now;
+  }
+
+  return ret;
 }
 
 //
-// This is especially dirty. 
+// This is especially dirty.
 //
 
-char *strMsTimestamp(const T_timestamp &ts)
+std::string strMsTimestamp(const T_timestamp &ts)
 {
-  char *ctime_now = ctime((time_t *) &ts.tv_sec);
+  std::string ret;
+
+  std::string ctime_now = strTimestamp(ts);
 
-  char ctime_new[25];
+  if (!(ctime_now.empty()))
+  {
+    char ctime_new[26] = { };
 
-  sprintf(ctime_new, "%.8s:%3.3f", ctime_now + 11,
-              (float) ts.tv_usec / 1000);
+    snprintf(ctime_new, sizeof(ctime_new), "%.8s:%3.3f",
+             ctime_now.c_str() + 11, static_cast<float>(ts.tv_usec) / 1000);
 
-  strncpy(ctime_now, ctime_new, 24);
+    ret = ctime_new;
+  }
 
-  return ctime_now;
+  return ret;
 }
diff --git a/nxcomp/src/Timestamp.h b/nxcomp/src/Timestamp.h
index bb9b243..9e6fafc 100644
--- a/nxcomp/src/Timestamp.h
+++ b/nxcomp/src/Timestamp.h
@@ -26,11 +26,17 @@
 #ifndef Timestamp_H
 #define Timestamp_H
 
+#if HAVE_CTIME_S
+#define __STDC_WANT_LIB_EXT1__ 1
+#include <time.h>
+#endif /* HAVE_CTIME_S */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <iostream>
+#include <string>
 
-#include <time.h>
 #include <sys/time.h>
 
 #include "Misc.h"
@@ -260,15 +266,15 @@ inline int checkDiffTimestamp(const T_timestamp &ts1, const T_timestamp &ts2,
 // Return a string representing the timestamp.
 //
 
-char *strTimestamp(const T_timestamp &ts);
-char *strMsTimestamp(const T_timestamp &ts);
+std::string strTimestamp(const T_timestamp &ts);
+std::string strMsTimestamp(const T_timestamp &ts);
 
-inline char *strTimestamp()
+inline std::string strTimestamp()
 {
   return strTimestamp(getTimestamp());
 }
 
-inline char *strMsTimestamp()
+inline std::string strMsTimestamp()
 {
   return strMsTimestamp(getTimestamp());
 }

--
Alioth's /home/x2go-admin/maintenancescripts/git/hooks/post-receive-email on /srv/git/code.x2go.org/nx-libs.git


More information about the x2go-commits mailing list