]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
[Bug 3556] ntp_loopfilter.c snprintf compilation warnings
authorJuergen Perlinger <perlinger@ntp.org>
Sat, 1 Dec 2018 11:10:51 +0000 (12:10 +0100)
committerJuergen Perlinger <perlinger@ntp.org>
Sat, 1 Dec 2018 11:10:51 +0000 (12:10 +0100)
 - provide better function for incremental string formatting

bk: 5c026c3bFcFiwk5XT5kAcXgiQbNFoA

14 files changed:
ChangeLog
include/ntp_stdlib.h
libntp/Makefile.am
libntp/xsbprintf.c [new file with mode: 0644]
ntpd/ntp_loopfilter.c
ports/winnt/vs2005/libntp.vcproj
ports/winnt/vs2008/libntp/libntp.vcproj
ports/winnt/vs2013/libntp/libntp.vcxproj
ports/winnt/vs2013/libntp/libntp.vcxproj.filters
ports/winnt/vs2015/libntp/libntp.vcxproj
ports/winnt/vs2015/libntp/libntp.vcxproj.filters
tests/libntp/Makefile.am
tests/libntp/run-sbprintf.c [new file with mode: 0644]
tests/libntp/sbprintf.c [new file with mode: 0644]

index f381a093cf948c1202162000eaf2d8abbf1a1dcf..1f7c18c543bfbbf8cb4092fedfedaba8e35758ad 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+---
+* [Bug 3556] ntp_loopfilter.c snprintf compilation warnings <perlinger@ntp.org>
+  - provide better function for incremental string formatting
+
 ---
 (4.2.8p12) 2018/08/14 Released by Harlan Stenn <stenn@ntp.org>
 
index 889c3b25ef4214f61fa35347d75fee9580f2edb6..c0bc71b09f5ba947833f604610a8df306991bbfa 100644 (file)
@@ -40,6 +40,9 @@ extern        void    setup_logfile   (const char *);
 extern void    errno_to_str(int, char *, size_t);
 #endif
 
+extern int     xvsbprintf(char**, char* const, char const*, va_list) NTP_PRINTF(3, 0);
+extern int     xsbprintf(char**, char* const, char const*, ...) NTP_PRINTF(3, 4);
+
 /*
  * When building without OpenSSL, use a few macros of theirs to
  * minimize source differences in NTP.
index 6f3c0ba459e3f04548ec73c6266b29eff7180332..04b53b0cbe971506c3aa4b7a5b68778fea1052d4 100644 (file)
@@ -110,6 +110,7 @@ libntp_a_SRCS =                                             \
        vint64ops.c                                     \
        work_fork.c                                     \
        work_thread.c                                   \
+       xsbprintf.c                                     \
        ymd2yd.c                                        \
        $(libisc_SRCS)                                  \
        $(NULL)
diff --git a/libntp/xsbprintf.c b/libntp/xsbprintf.c
new file mode 100644 (file)
index 0000000..e7d1975
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * xsbprintf.c - string buffer formatting helpers
+ *
+ * Written by Juergen Perlinger (perlinger@ntp.org) for the NTP project.
+ * The contents of 'html/copyright.html' apply.
+ */
+
+#include <config.h>
+#include <sys/types.h>
+
+#include "ntp_stdlib.h"
+
+/* eXtended Varlist String Buffer printf
+ *
+ * Formats via 'vsnprintf' into a string buffer, with some semantic
+ * specialties:
+ *
+ * - The start of the buffer pointer is updated according to the number
+ *   of characters written.
+ * - If the buffer is insufficient to format the number of charactes,
+ *   the partial result will be be discarded, and zero is returned to
+ *   indicate nothing was written to the buffer.
+ * - On successful formatting, the return code is the return value of
+ *   the inner call to 'vsnprintf()'.
+ * - If there is any error, the state of the buffer will not be
+ *   changed. (Bytes in the buffer might be smashed, but the buffer
+ *   position does not change, and the NUL marker stays in place at the
+ *   current buffer position.)
+ */
+int
+xvsbprintf(
+       char       **ppbuf,     /* pointer to buffer pointer (I/O) */
+       char * const pend,      /* buffer end (I)                  */
+       char const  *pfmt,      /* printf-like format string       */
+       va_list      va         /* formatting args for above       */
+       )
+{
+       char *pbuf = (ppbuf) ? *ppbuf : NULL;
+       int   rc   = -1;
+       if (pbuf) {
+               size_t ilen = (size_t)(pend - pbuf);
+               if (ilen) {
+                       *pbuf = '\0';
+                       rc    = vsnprintf(pbuf, ilen, pfmt, va);
+                       if (rc > 0) {
+                               if ((size_t)rc >= ilen)
+                                       rc = 0;
+                               pbuf += rc;
+                       }
+                       *pbuf = '\0'; /* fear of bad vsnprintf */
+               }               
+               *ppbuf = pbuf;
+       } else {
+               errno = EINVAL;
+       }
+       return rc;
+}
+
+/* variadic wrapper around the buufer string formatter */
+int
+xsbprintf(
+       char       **ppbuf,     /* pointer to buffer pointer (I/O) */
+       char * const pend,      /* buffer end (I)                  */
+       char const  *pfmt,      /* printf-like format string       */
+       ...                     /* formatting args for above       */
+       )
+{
+       va_list va;
+       int     rc;
+       
+       va_start(va, pfmt);
+       rc = xvsbprintf(ppbuf, pend, pfmt, va);
+       va_end(va);
+       return rc;
+}
+
+/* that's all folks! */
index 924d5738b1e47c0694128454a9f312ec8066b87d..01772bd1ffe9e1bdcdc0a63ff5afbf3b47862f7e 100644 (file)
@@ -246,7 +246,11 @@ ntp_adjtime_error_handler(
        )
 {
        char des[1024] = "";    /* Decoded Error Status */
+       char *dbp, *ebp;
 
+       dbp = des;
+       ebp = dbp + sizeof(des);
+       
        switch (ret) {
            case -1:
                switch (saved_errno) {
@@ -363,37 +367,37 @@ or, from ntp_adjtime():
                                /* error (see status word) */
 
                if (ptimex->status & STA_UNSYNC)
-                       snprintf(des, sizeof(des), "%s%sClock Unsynchronized",
-                               des, (*des) ? "; " : "");
+                       xsbprintf(&dbp, ebp, "%sClock Unsynchronized",
+                                (*des) ? "; " : "");
 
                if (ptimex->status & STA_CLOCKERR)
-                       snprintf(des, sizeof(des), "%s%sClock Error",
-                               des, (*des) ? "; " : "");
+                   xsbprintf(&dbp, ebp, "%sClock Error",
+                             (*des) ? "; " : "");
 
                if (!(ptimex->status & STA_PPSSIGNAL)
                    && ptimex->status & STA_PPSFREQ)
-                       snprintf(des, sizeof(des), "%s%sPPS Frequency Sync wanted but no PPS",
-                               des, (*des) ? "; " : "");
+                   xsbprintf(&dbp, ebp, "%sPPS Frequency Sync wanted but no PPS",
+                             (*des) ? "; " : "");
 
                if (!(ptimex->status & STA_PPSSIGNAL)
                    && ptimex->status & STA_PPSTIME)
-                       snprintf(des, sizeof(des), "%s%sPPS Time Sync wanted but no PPS signal",
-                               des, (*des) ? "; " : "");
+                       xsbprintf(&dbp, ebp, "%sPPS Time Sync wanted but no PPS signal",
+                                 (*des) ? "; " : "");
 
                if (   ptimex->status & STA_PPSTIME
                    && ptimex->status & STA_PPSJITTER)
-                       snprintf(des, sizeof(des), "%s%sPPS Time Sync wanted but PPS Jitter exceeded",
-                               des, (*des) ? "; " : "");
+                       xsbprintf(&dbp, ebp, "%sPPS Time Sync wanted but PPS Jitter exceeded",
+                                 (*des) ? "; " : "");
 
                if (   ptimex->status & STA_PPSFREQ
                    && ptimex->status & STA_PPSWANDER)
-                       snprintf(des, sizeof(des), "%s%sPPS Frequency Sync wanted but PPS Wander exceeded",
-                               des, (*des) ? "; " : "");
+                       xsbprintf(&dbp, ebp, "%sPPS Frequency Sync wanted but PPS Wander exceeded",
+                                 (*des) ? "; " : "");
 
                if (   ptimex->status & STA_PPSFREQ
                    && ptimex->status & STA_PPSERROR)
-                       snprintf(des, sizeof(des), "%s%sPPS Frequency Sync wanted but Calibration error detected",
-                               des, (*des) ? "; " : "");
+                       xsbprintf(&dbp, ebp, "%sPPS Frequency Sync wanted but Calibration error detected",
+                                 (*des) ? "; " : "");
 
                if (pps_call && !(ptimex->status & STA_PPSSIGNAL))
                        report_event(EVNT_KERN, NULL,
index e1c7b659558c5e7c77bfb4dd186d0e0ea3681b66..312ccdac0eea0cf537dfc82869fb4776fa0acb9b 100644 (file)
                                RelativePath="..\..\..\libntp\work_thread.c"
                                >
                        </File>
+                       <File
+                               RelativePath="..\..\..\libntp\xsbprintf.c"
+                               >
+                       </File>
                        <File
                                RelativePath="..\..\..\libntp\ymd2yd.c"
                                >
index 2057d758b6760e0dda04c5251033d2d4ad9e6bb9..7a7b478aecbf408ca91da8c30defac55445c6801 100644 (file)
                                RelativePath="..\..\..\..\libntp\work_thread.c"
                                >
                        </File>
+                       <File
+                               RelativePath="..\..\..\..\libntp\xsbprintf.c"
+                               >
+                       </File>
                        <File
                                RelativePath="..\..\..\..\libntp\ymd2yd.c"
                                >
index 26a13c21f7555767440172e28d3830182991f192..3d9f295c7347f524798fbff99a97dcfbf897c960 100644 (file)
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
 <Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <ItemGroup Label="ProjectConfigurations">
     <ProjectConfiguration Include="DebugXP|Win32">
     <ClCompile Include="..\..\..\..\libntp\vint64ops.c" />
     <ClCompile Include="..\..\..\..\libntp\work_fork.c" />
     <ClCompile Include="..\..\..\..\libntp\work_thread.c" />
+    <ClCompile Include="..\..\..\..\libntp\xsbprintf.c" />
     <ClCompile Include="..\..\..\..\libntp\ymd2yd.c" />
     <ClCompile Include="..\..\..\..\lib\isc\assertions.c" />
     <ClCompile Include="..\..\..\..\lib\isc\backtrace-emptytbl.c" />
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
\ No newline at end of file
+</Project>
index 9839bbd8349acd78dec143b002d7b3b2bcd801bf..1d6a7b40f13354f7b46786cc53c6b3d2daa48c2c 100644 (file)
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <ItemGroup>
     <Filter Include="Source Files">
     <ClCompile Include="..\..\..\..\libntp\work_thread.c">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="..\..\..\..\libntp\xsbprintf.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
     <ClCompile Include="..\..\..\..\libntp\ymd2yd.c">
       <Filter>Source Files</Filter>
     </ClCompile>
       <Filter>Resource Files</Filter>
     </CustomBuild>
   </ItemGroup>
-</Project>
\ No newline at end of file
+</Project>
index a93e97015f988c3d4ff921bcf543268c3f4db0cf..0a9433f328434f255701c570fb157ee580cc7f4e 100644 (file)
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
 <Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <ItemGroup Label="ProjectConfigurations">
     <ProjectConfiguration Include="DebugXP|Win32">
     <ClCompile Include="..\..\..\..\libntp\vint64ops.c" />
     <ClCompile Include="..\..\..\..\libntp\work_fork.c" />
     <ClCompile Include="..\..\..\..\libntp\work_thread.c" />
+    <ClCompile Include="..\..\..\..\libntp\xsbprintf.c" />
     <ClCompile Include="..\..\..\..\libntp\ymd2yd.c" />
     <ClCompile Include="..\..\..\..\lib\isc\assertions.c" />
     <ClCompile Include="..\..\..\..\lib\isc\backtrace-emptytbl.c" />
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
-</Project>
\ No newline at end of file
+</Project>
index 9839bbd8349acd78dec143b002d7b3b2bcd801bf..1d6a7b40f13354f7b46786cc53c6b3d2daa48c2c 100644 (file)
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <ItemGroup>
     <Filter Include="Source Files">
     <ClCompile Include="..\..\..\..\libntp\work_thread.c">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="..\..\..\..\libntp\xsbprintf.c">
+      <Filter>Source Files</Filter>
+    </ClCompile>
     <ClCompile Include="..\..\..\..\libntp\ymd2yd.c">
       <Filter>Source Files</Filter>
     </ClCompile>
       <Filter>Resource Files</Filter>
     </CustomBuild>
   </ItemGroup>
-</Project>
\ No newline at end of file
+</Project>
index 6ebb1170582a41902b5fde291a1bab5caa10e7c2..67b34e7c0fc70c9af2ed4203b40313e1b58a2938 100644 (file)
@@ -40,6 +40,7 @@ check_PROGRAMS =              \
        test-recvbuff           \
        test-refidsmear         \
        test-refnumtoa          \
+       test-sbprintf           \
        test-sfptostr           \
        test-socktoa            \
        test-ssl_init           \
@@ -104,6 +105,7 @@ BUILT_SOURCES +=                    \
        $(srcdir)/run-recvbuff.c        \
        $(srcdir)/run-refidsmear.c      \
        $(srcdir)/run-refnumtoa.c       \
+       $(srcdir)/run-sbprintf.c        \
        $(srcdir)/run-sfptostr.c        \
        $(srcdir)/run-socktoa.c         \
        $(srcdir)/run-ssl_init.c        \
@@ -410,6 +412,16 @@ $(srcdir)/run-refnumtoa.c: $(srcdir)/refnumtoa.c $(std_unity_list)
 
 ###
 
+test_sbprintf_SOURCES =                \
+       sbprintf.c              \
+       run-sbprintf.c          \
+       $(NULL)
+
+$(srcdir)/run-sbprintf.c: $(srcdir)/sbprintf.c $(std_unity_list)
+       $(run_unity) $< $@
+
+###
+
 test_sfptostr_SOURCES =                \
        sfptostr.c              \
        run-sfptostr.c          \
diff --git a/tests/libntp/run-sbprintf.c b/tests/libntp/run-sbprintf.c
new file mode 100644 (file)
index 0000000..a3c1661
--- /dev/null
@@ -0,0 +1,72 @@
+/* AUTOGENERATED FILE. DO NOT EDIT. */
+
+//=======Test Runner Used To Run Each Test Below=====
+#define RUN_TEST(TestFunc, TestLineNum) \
+{ \
+  Unity.CurrentTestName = #TestFunc; \
+  Unity.CurrentTestLineNumber = TestLineNum; \
+  Unity.NumberOfTests++; \
+  if (TEST_PROTECT()) \
+  { \
+      setUp(); \
+      TestFunc(); \
+  } \
+  if (TEST_PROTECT() && !TEST_IS_IGNORED) \
+  { \
+    tearDown(); \
+  } \
+  UnityConcludeTest(); \
+}
+
+//=======Automagically Detected Files To Include=====
+#include "unity.h"
+#include <setjmp.h>
+#include <stdio.h>
+#include "config.h"
+#include "ntp_stdlib.h"
+#include <errno.h>
+
+//=======External Functions This Runner Calls=====
+extern void setUp(void);
+extern void tearDown(void);
+extern void test_NullBuf1(void);
+extern void test_NullBuf2(void);
+extern void test_SmallBuf(void);
+extern void test_MatchBuf(void);
+extern void test_BigBuf(void);
+extern void test_SimpleArgs(void);
+
+
+//=======Suite Setup=====
+static void suite_setup(void)
+{
+extern int change_logfile(const char*, int);
+change_logfile("stderr", 0);
+}
+
+//=======Test Reset Option=====
+void resetTest(void);
+void resetTest(void)
+{
+  tearDown();
+  setUp();
+}
+
+char const *progname;
+
+
+//=======MAIN=====
+int main(int argc, char *argv[])
+{
+  progname = argv[0];
+  suite_setup();
+  UnityBegin("sbprintf.c");
+  RUN_TEST(test_NullBuf1, 7);
+  RUN_TEST(test_NullBuf2, 14);
+  RUN_TEST(test_SmallBuf, 23);
+  RUN_TEST(test_MatchBuf, 34);
+  RUN_TEST(test_BigBuf, 45);
+  RUN_TEST(test_SimpleArgs, 56);
+
+  return (UnityEnd());
+}
diff --git a/tests/libntp/sbprintf.c b/tests/libntp/sbprintf.c
new file mode 100644 (file)
index 0000000..6df4263
--- /dev/null
@@ -0,0 +1,67 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+#include "unity.h"
+
+#include <errno.h>
+
+extern void test_NullBuf1(void);
+void test_NullBuf1(void)
+{
+       int rc = xsbprintf(NULL, NULL, "blah");
+        TEST_ASSERT(rc == -1 && errno == EINVAL);
+}
+
+extern void test_NullBuf2(void);
+void test_NullBuf2(void)
+{
+       char *bp = NULL;
+       int   rc = xsbprintf(&bp, NULL, "blah");
+        TEST_ASSERT(rc == -1 && errno == EINVAL);
+       TEST_ASSERT_EQUAL_PTR(bp, NULL);
+}
+
+extern void test_SmallBuf(void);
+void test_SmallBuf(void)
+{
+       char  ba[4];
+       char *bp = ba;
+       char *ep = ba + sizeof(ba);
+       int   rc = xsbprintf(&bp, ep, "1234");
+        TEST_ASSERT(rc == 0 && strlen(ba) == 0);
+       TEST_ASSERT_EQUAL_PTR(bp, ba);
+}
+
+extern void test_MatchBuf(void);
+void test_MatchBuf(void)
+{
+       char  ba[5];
+       char *bp = ba;
+       char *ep = ba + sizeof(ba);
+       int   rc = xsbprintf(&bp, ep, "1234");
+        TEST_ASSERT(rc == 4 && strlen(ba) == 4);
+       TEST_ASSERT_EQUAL_PTR(bp, ba + 4);
+}
+
+extern void test_BigBuf(void);
+void test_BigBuf(void)
+{
+       char  ba[10];
+       char *bp = ba;
+       char *ep = ba + sizeof(ba);
+       int   rc = xsbprintf(&bp, ep, "1234");
+        TEST_ASSERT(rc == 4 && strlen(ba) == 4);
+       TEST_ASSERT_EQUAL_PTR(bp, ba + 4);
+}
+
+extern void test_SimpleArgs(void);
+void test_SimpleArgs(void)
+{
+       char  ba[10];
+       char *bp = ba;
+       char *ep = ba + sizeof(ba);
+       int   rc = xsbprintf(&bp, ep, "%d%d%d%d", 1, 2, 3, 4);
+        TEST_ASSERT(rc == 4 && strlen(ba) == 4);
+       TEST_ASSERT_EQUAL_PTR(bp, ba + 4);
+       TEST_ASSERT_FALSE(strcmp(ba, "1234"));
+}
+