← Back to team overview

kicad-developers team mailing list archive

[PATCH] correct some snprintf usage

 

Fixes some cases where n-1 bytes was used as a size but snprintf
protects against that internally. Also some hardcoded constant sizes
changed to sizeof()

And made a case where sprintf was used into snprintf as the path input
as aUserPluginsPath could in theory be very stupid long if someone has
a very stupid filesystem. Would be best if there was error handling if
it exceeds ( snprintf will return greater than sizeof(buf) ) but I
have no clue how that section needs cleanup.
From aa1ed81a627cb16727ecf66473e1009723b8f934 Mon Sep 17 00:00:00 2001
From: Mark Roszko <mark.roszko@xxxxxxxxx>
Date: Sat, 21 Feb 2015 21:55:35 -0500
Subject: [PATCH] snprintf usage correction, it always accounts for n-1 buffer
 size and also use sizeof

---
 eeschema/sch_item_struct.cpp   | 10 ++++------
 lib_dxf/libdxfrw.cpp           |  2 +-
 scripting/python_scripting.cpp |  4 ++--
 3 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/eeschema/sch_item_struct.cpp b/eeschema/sch_item_struct.cpp
index 9a2cda7..047d5c8 100644
--- a/eeschema/sch_item_struct.cpp
+++ b/eeschema/sch_item_struct.cpp
@@ -2,7 +2,7 @@
  * This program source code file is part of KiCad, a free EDA CAD application.
  *
  * Copyright (C) 2006 Jean-Pierre Charras, jaen-pierre.charras@xxxxxxxxxxxxxxxxxx
- * Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.txt for contributors.
+ * Copyright (C) 1992-2015 KiCad Developers, see AUTHORS.txt for contributors.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -112,8 +112,7 @@ std::string SCH_ITEM::FormatInternalUnits( int aValue )
 
     if( engUnits != 0.0 && fabs( engUnits ) <= 0.0001 )
     {
-        // printf( "f: " );
-        len = snprintf( buf, 49, "%.10f", engUnits );
+        len = snprintf( buf, sizeof(buf), "%.10f", engUnits );
 
         while( --len > 0 && buf[len] == '0' )
             buf[len] = '\0';
@@ -122,8 +121,7 @@ std::string SCH_ITEM::FormatInternalUnits( int aValue )
     }
     else
     {
-        // printf( "g: " );
-        len = snprintf( buf, 49, "%.10g", engUnits );
+        len = snprintf( buf, sizeof(buf), "%.10g", engUnits );
     }
 
     return std::string( buf, len );
@@ -135,7 +133,7 @@ std::string SCH_ITEM::FormatAngle( double aAngle )
     char temp[50];
     int len;
 
-    len = snprintf( temp, 49, "%.10g", aAngle / 10.0 );
+    len = snprintf( temp, sizeof(temp), "%.10g", aAngle / 10.0 );
 
     return std::string( temp, len );
 }
diff --git a/lib_dxf/libdxfrw.cpp b/lib_dxf/libdxfrw.cpp
index 2390b38..ce4c7a5 100644
--- a/lib_dxf/libdxfrw.cpp
+++ b/lib_dxf/libdxfrw.cpp
@@ -3818,7 +3818,7 @@ std::string dxfRW::toHexStr( int n )
 {
 #if defined(__APPLE__)
     char buffer[9] = { '\0' };
-    snprintf( buffer, 9, "%X", n );
+    snprintf( buffer, sizeof(buffer), "%X", n );
     return std::string( buffer );
 #else
     std::ostringstream Convert;
diff --git a/scripting/python_scripting.cpp b/scripting/python_scripting.cpp
index 15f0d6c..0f8072e 100644
--- a/scripting/python_scripting.cpp
+++ b/scripting/python_scripting.cpp
@@ -144,7 +144,7 @@ bool pcbnewInitPythonScripting( const char * aUserPluginsPath )
     // Make sure that that the correct version of wxPython is loaded. In systems where there
     // are different versions of wxPython installed this can lead to select wrong wxPython
     // version being selected.
-    snprintf( cmd, 1023, "import wxversion; wxversion.select('%s')", WXPYTHON_VERSION );
+    snprintf( cmd, sizeof(cmd), "import wxversion; wxversion.select('%s')", WXPYTHON_VERSION );
     PyRun_SimpleString( cmd );
 
     // Load the wxPython core API.  Imports the wx._core_ module and sets a
@@ -168,7 +168,7 @@ bool pcbnewInitPythonScripting( const char * aUserPluginsPath )
     {
         char cmd[1024];
         PyLOCK lock;
-        sprintf( cmd, "import sys, traceback\n"
+        snprintf( cmd, sizeof(cmd), "import sys, traceback\n"
                       "sys.path.append(\".\")\n"
                       "import pcbnew\n"
                       "pcbnew.LoadPlugins(\"%s\")", aUserPluginsPath );
-- 
1.9.1


Follow ups