Improve the mysprintf() issue in openvpnserv.c
PATCH 1/1 Improve the mysprintf() issue in openvpnserv.c
From: David Sommerseth <[email protected]...>
A quick and dirty compile fix was introduced in commit 77d244050964525417, and was accepted under the condition that it would be a temporary fix.
As the usage of _snprintf() is realy not ideal on Windows, this patch uses the same well tested openvpn_snprintf() function from buffer.c. It was a longer discussion of several possibilities to re-use that code, but in the end it seemed easier to just copy-paste this function to openvpnserv.c for now.
The reason for this conclusion was that the function is really simple, well defined and will most likely not be changed much in the future. It is also added a comment in openvpnserv.c where this function has its origins.
--- service-win32/openvpnserv.c | 39 ++++++++++++++++++++++++++++----------- 1 files changed, 28 insertions(+), 11 deletions(-)
service-win32/openvpnserv.c +28 -11
@@ -37,6 +37,7 @@#include <windows.h> #include <stdlib.h> #include <stdio.h> +#include <stdarg.h> #include <process.h> #include "service.h"
@@ -79,13 +80,6 @@ static HANDLE exit_event = NULL;/* clear an object */ #define CLEAR (x) memset(&(x), 0, sizeof(x)) -/* snprintf with guaranteed null termination */ -#define mysnprintf (out, ...) \ - { \ - _snprintf (out, sizeof(out), __VA_ARGS__); \ - out [sizeof (out) - 1] = '\0'; \ - } -/* * Message handling */
@@ -97,7 +91,7 @@ static HANDLE exit_event = NULL;#define MSG (flags, ...) \ { \ char x_msg[256]; \ - mysnprintf (x_msg, __VA_ARGS__); \ + openvpn_snprintf (x_msg, sizeof(x_msg), __VA_ARGS__); \ AddToMessageLog ((flags), x_msg); \ }
@@ -129,6 +123,28 @@ static HANDLE exit_event = NULL; } \ } +/* +* This is necessary due to certain buggy implementations of snprintf, +* that don't guarantee null termination for size > 0. +* (copied from ../buffer.c, line 217) +* (git: 100644 blob e2f8caab0a5b2a870092c6cd508a1a50c21c3ba3 buffer.c) +*/ + +int openvpn_snprintf (char *str,size_t size,const char *format, ...) +{ +va_list arglist; +int ret =0 ; +if (size >0 ) + { +va_start (arglist, format); + ret =vsnprintf (str, size, format, arglist); +va_end (arglist); + str[size -1 ] =0 ; + } +return ret; +} + +bool init_security_attributes_allow_all (struct security_attributes *obj) {
@@ -353,7 +369,7 @@ VOID ServiceStart (DWORD dwArgc, LPTSTR *lpszArgv)BOOL more_files;char find_string[MAX_PATH ]; -mysnprintf (find_string,"%s\\*" , config_dir); +openvpn_snprintf (find_string,MAX_PATH ,"%s\\*" , config_dir); find_handle =FindFirstFile (find_string, &find_obj);if (find_handle ==INVALID_HANDLE_VALUE )
@@ -395,10 +411,11 @@ VOID ServiceStart (DWORD dwArgc, LPTSTR *lpszArgv)FindClose (find_handle); goto finish; } -mysnprintf (log_path,"%s\\%s" , log_dir, log_file); +openvpn_snprintf (log_path,sizeof (log_path), +"%s\\%s" , log_dir, log_file);/* construct command line */ -mysnprintf (command_line,PACKAGE " --service %s 1 --config \"%s\"" , +openvpn_snprintf (command_line,sizeof (command_line),PACKAGE " --service %s 1 --config \"%s\"" ,EXIT_EVENT_NAME , find_obj.cFileName);
Signature · 1 line
1.7.4.4
Quoted 1 line · David Sommerseth
Signed-off-by David Sommerseth <[email protected]...>
David Sommerseth
Applied-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Quoted 16 lines · David Sommerseth
From: David Sommerseth <[email protected]...>
A quick and dirty compile fix was introduced in commit
77d244050964525417, and was accepted under the condition that it would be a temporary fix.As the usage of
_snprintf()is realy not ideal on Windows, this patch uses the same well testedopenvpn_snprintf()function frombuffer.c. It was a longer discussion of several possibilities to re-use that code, but in the end it seemed easier to just copy-paste this function toopenvpnserv.cfor now.The reason for this conclusion was that the function is really simple, well defined and will most likely not be changed much in the future. It is also added a comment in
openvpnserv.cwhere this function has its origins.
Quoted 4 lines
Signed-off-by David Sommerseth <[email protected]...>--- service-win32/openvpnserv.c | 39 ++++++++++++++++++++++++++++----------- 1 files changed, 28 insertions(+), 11 deletions(-)
ACKed by James Yonan on IRC 2011-04-11 18:51 UTC:
<dazo> jamesyonan: do you have time to have a look at this patch? and ACK it if you agree ... <http://thread.gmane.org/gmane.network.openvpn.devel/4621>
<jamesyonan> looking at it now...
<jamesyonan> dazo: sure, looks fine
Applied to master and cherry-picked to beta2.2.
commit 47dd9a4d7b2930f6e1b5d6f0ce0c154efc87ac29 (beta2.2) commit df5a4380c3931520d5fae2b18f0fc2e67a883aae (master)
Author: David Sommerseth <[email protected]...> Date: Thu Apr 21 20:32:26 2011 +0200
Improve the mysprintf() issue in openvpnserv.c
Signed-off-by: David Sommerseth <[email protected]...>
Acked-by: James Yonan <[email protected]...>kind regards,
David Sommerseth
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/
iEYEARECAAYFAk2wgLMACgkQDC186MBRfrpHowCgnEQbjP2foyBbNtHcxULaVd4B MzEAn02/K92yp6ujE3d39gLpngghV7YG
=jMRx
-----END PGP SIGNATURE-----