qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cliffjan...@apache.org
Subject svn commit: r1512109 - in /qpid/proton/trunk: proton-c/ proton-c/src/ proton-c/src/codec/ proton-c/src/engine/ proton-c/src/object/ tools/cmake/Modules/
Date Fri, 09 Aug 2013 00:17:55 GMT
Author: cliffjansen
Date: Fri Aug  9 00:17:54 2013
New Revision: 1512109

URL: http://svn.apache.org/r1512109
Log:
proton-212 - fix and check for non C99 compliant Windows functions

Added:
    qpid/proton/trunk/tools/cmake/Modules/WindowsC99CheckDef.cmake
    qpid/proton/trunk/tools/cmake/Modules/WindowsC99SymbolCheck.py
Modified:
    qpid/proton/trunk/proton-c/CMakeLists.txt
    qpid/proton/trunk/proton-c/src/codec/codec.c
    qpid/proton/trunk/proton-c/src/engine/engine.c
    qpid/proton/trunk/proton-c/src/error.c
    qpid/proton/trunk/proton-c/src/object/object.c
    qpid/proton/trunk/proton-c/src/platform.c
    qpid/proton/trunk/proton-c/src/platform.h

Modified: qpid/proton/trunk/proton-c/CMakeLists.txt
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/proton-c/CMakeLists.txt?rev=1512109&r1=1512108&r2=1512109&view=diff
==============================================================================
--- qpid/proton/trunk/proton-c/CMakeLists.txt (original)
+++ qpid/proton/trunk/proton-c/CMakeLists.txt Fri Aug  9 00:17:54 2013
@@ -284,6 +284,11 @@ set_target_properties (
   LINK_FLAGS "${CATCH_UNDEFINED}"
   )
 
+if (MSVC)
+  # guard against use of C99 violating functions on Windows
+  include(WindowsC99CheckDef)
+endif(MSVC)
+
 add_executable (proton src/proton.c)
 target_link_libraries (proton qpid-proton)
 

Modified: qpid/proton/trunk/proton-c/src/codec/codec.c
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/proton-c/src/codec/codec.c?rev=1512109&r1=1512108&r2=1512109&view=diff
==============================================================================
--- qpid/proton/trunk/proton-c/src/codec/codec.c (original)
+++ qpid/proton/trunk/proton-c/src/codec/codec.c Fri Aug  9 00:17:54 2013
@@ -32,6 +32,7 @@
 #include "encodings.h"
 #define DEFINE_FIELDS
 #include "protocol.h"
+#include "../platform.h"
 #include "../platform_fmt.h"
 #include "../util.h"
 

Modified: qpid/proton/trunk/proton-c/src/engine/engine.c
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/proton-c/src/engine/engine.c?rev=1512109&r1=1512108&r2=1512109&view=diff
==============================================================================
--- qpid/proton/trunk/proton-c/src/engine/engine.c (original)
+++ qpid/proton/trunk/proton-c/src/engine/engine.c Fri Aug  9 00:17:54 2013
@@ -31,6 +31,7 @@
 
 #include "../sasl/sasl-internal.h"
 #include "../ssl/ssl-internal.h"
+#include "../platform.h"
 #include "../platform_fmt.h"
 
 static ssize_t transport_consume(pn_transport_t *transport);

Modified: qpid/proton/trunk/proton-c/src/error.c
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/proton-c/src/error.c?rev=1512109&r1=1512108&r2=1512109&view=diff
==============================================================================
--- qpid/proton/trunk/proton-c/src/error.c (original)
+++ qpid/proton/trunk/proton-c/src/error.c Fri Aug  9 00:17:54 2013
@@ -23,6 +23,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include "util.h"
+#include "platform.h"
 
 struct pn_error_t {
   int code;

Modified: qpid/proton/trunk/proton-c/src/object/object.c
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/proton-c/src/object/object.c?rev=1512109&r1=1512108&r2=1512109&view=diff
==============================================================================
--- qpid/proton/trunk/proton-c/src/object/object.c (original)
+++ qpid/proton/trunk/proton-c/src/object/object.c Fri Aug  9 00:17:54 2013
@@ -19,6 +19,7 @@
  *
  */
 
+#include "../platform.h"
 #include <proton/error.h>
 #include <proton/object.h>
 #include <stdio.h>

Modified: qpid/proton/trunk/proton-c/src/platform.c
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/proton-c/src/platform.c?rev=1512109&r1=1512108&r2=1512109&view=diff
==============================================================================
--- qpid/proton/trunk/proton-c/src/platform.c (original)
+++ qpid/proton/trunk/proton-c/src/platform.c Fri Aug  9 00:17:54 2013
@@ -133,3 +133,32 @@ int64_t pn_i_atoll(const char* num) {
 #else
 #error "Don't know how to convert int64_t values on this platform"
 #endif
+
+#ifdef _MSC_VER
+// [v]snprintf on Windows only matches C99 when no errors or overflow.
+int pn_i_vsnprintf(char *buf, size_t count, const char *fmt, va_list ap) {
+  if (fmt == NULL)
+    return -1;
+  if ((buf == NULL) && (count > 0))
+    return -1;
+  if (count > 0) {
+    int n = vsnprintf_s(buf, count, _TRUNCATE, fmt, ap);
+    if (n >= 0)  // no overflow
+      return n;  // same as C99
+    buf[count-1] = '\0';
+  }
+  // separate call to get needed buffer size on overflow
+  int n = _vscprintf(fmt, ap);
+  if (n >= (int) count)
+    return n;
+  return -1;
+}
+
+int pn_i_snprintf(char *buf, size_t count, const char *fmt, ...) {
+  va_list ap;
+  va_start(ap, fmt);
+  int n = pn_i_vsnprintf(buf, count, fmt, ap);
+  va_end(ap);
+  return n;
+}
+#endif

Modified: qpid/proton/trunk/proton-c/src/platform.h
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/proton-c/src/platform.h?rev=1512109&r1=1512108&r2=1512109&view=diff
==============================================================================
--- qpid/proton/trunk/proton-c/src/platform.h (original)
+++ qpid/proton/trunk/proton-c/src/platform.h Fri Aug  9 00:17:54 2013
@@ -73,8 +73,15 @@ int pn_i_error_from_errno(pn_error_t *er
 int64_t pn_i_atoll(const char* num);
 
 #ifdef _MSC_VER
-// TODO: PROTON-212
-#define snprintf _snprintf
+/** Windows snprintf and vsnprintf substitutes.
+ *
+ * Provide the expected C99 behavior for these functions.
+ */
+#include <stdio.h>
+#define snprintf pn_i_snprintf
+#define vsnprintf pn_i_vsnprintf
+int pn_i_snprintf(char *buf, size_t count, const char *fmt, ...);
+int pn_i_vsnprintf(char *buf, size_t count, const char *fmt, va_list ap);
 #endif
 
 

Added: qpid/proton/trunk/tools/cmake/Modules/WindowsC99CheckDef.cmake
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/tools/cmake/Modules/WindowsC99CheckDef.cmake?rev=1512109&view=auto
==============================================================================
--- qpid/proton/trunk/tools/cmake/Modules/WindowsC99CheckDef.cmake (added)
+++ qpid/proton/trunk/tools/cmake/Modules/WindowsC99CheckDef.cmake Fri Aug  9 00:17:54 2013
@@ -0,0 +1,31 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+#
+# Check qpid-proton.dll after linking for dangerous calls to
+# Windows functions that suggest but deviate from C99 behavior:
+#   _snprintf, vsnprintf, _vsnprintf
+# See platform.h for safe wrapper calls.
+#
+
+set(obj_dir ${CMAKE_CURRENT_BINARY_DIR}/qpid-proton.dir/${CMAKE_CFG_INTDIR})
+
+add_custom_command(TARGET qpid-proton PRE_LINK COMMAND python
+        ${CMAKE_MODULE_PATH}WindowsC99SymbolCheck.py ${obj_dir}
+        COMMENT "Checking for dangerous use of C99-violating functions")

Added: qpid/proton/trunk/tools/cmake/Modules/WindowsC99SymbolCheck.py
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/tools/cmake/Modules/WindowsC99SymbolCheck.py?rev=1512109&view=auto
==============================================================================
--- qpid/proton/trunk/tools/cmake/Modules/WindowsC99SymbolCheck.py (added)
+++ qpid/proton/trunk/tools/cmake/Modules/WindowsC99SymbolCheck.py Fri Aug  9 00:17:54 2013
@@ -0,0 +1,66 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+#
+# Stop cmake build if pn_i_xxx substitute functions aren't used for
+# the dangererous non-complying [v]snprintf family.  A source of
+# painful bug-hunting.
+#
+# Each obj must be checked instead of just the dll since Visual Studio
+# sometimes inserts references to vsnprintf in DllMainCRTStartup,
+# causing false positives.
+#
+# bad: vsnprintf, __vsnprintf, _imp__vsnprintf, ...,  same for snprintf
+# OK:  vsnprintf_s, pn_i_vsnprintf
+#
+
+import sys
+import os
+import subprocess
+import glob
+import re
+
+def symcheck(objfile):
+
+    symfile = objfile.replace('.obj', '.sym')
+    cmd = ['dumpbin.exe', '/SYMBOLS', objfile, '/OUT:' + symfile]
+
+    # /dev/null standin
+    junk = open('junk', 'w')
+    p = subprocess.Popen(cmd, stdout=junk)
+    n = p.wait()
+    if n != 0 :
+        raise Exception("dumpbin call failure")
+
+    f = open(symfile, 'r')
+    for line in f :
+        m = re.search(r'UNDEF.*\b([a-zA-Z_]*snprintf)\b', line)
+        if m :
+            sym = m.group(1)
+            if re.match(r'_*pn_i_v?snprintf', sym) is None :
+                raise Exception('Unsafe use of C99 violating function in  ' + objfile + '
: ' + sym)
+
+def main():
+    os.chdir(sys.argv[1])
+    objs = glob.glob('*.obj')
+    for obj in glob.glob('*.obj'):
+        symcheck(obj)
+
+if __name__ == "__main__":
+    sys.exit(main())



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message