On Thu, 2014-01-30 at 14:58 +0100, Ferenc Wagner wrote:
> Svante Signell <[email protected]> writes:
> 
> > The attached patch fixes this problem by adding a check in
> > configure.ac for a working path = getcwd(NULL, 0) allocating the
> > string length required dynamically, and freed later on.  Similarly the
> > string baseURI is malloced and freed.
> 
> I can see the freeing of baseURI, but not that of path.
> I'd say it's leaked.  Or do I miss something here?

Damn, I updated the patch, viewed it in gedit and still the old patch
was there. Attached is the correct one.

Thanks!
---
 configure.ac                             |   11 +++++++++++
 xsec/tools/checksig/checksig.cpp         |   17 ++++++++++++-----
 xsec/tools/cipher/cipher.cpp             |   17 ++++++++++++-----
 xsec/tools/templatesign/templatesign.cpp |   17 ++++++++++++-----
 xsec/tools/txfmout/txfmout.cpp           |   18 ++++++++++++------
 5 files changed, 59 insertions(+), 21 deletions(-)

--- a/configure.ac
+++ b/configure.ac
@@ -73,6 +73,17 @@ AC_CHECK_HEADERS(unistd.h direct.h)
 
 AC_CHECK_DECL(strcasecmp,[AC_DEFINE([XSEC_HAVE_STRCASECMP],[1],[Define to 1 if strcasecmp present.])],,[#include <string.h>]) 
 
+# Check for required functionality
+AC_MSG_CHECKING([whether getcwd(NULL, 0) works])
+AC_RUN_IFELSE([AC_LANG_PROGRAM([#include <stdlib.h>
+ #include <unistd.h>],
+ [char *cwd = getcwd(NULL, 0);
+ return (cwd != NULL) ? EXIT_SUCCESS : EXIT_FAILURE;])],
+ [AC_MSG_RESULT(yes)
+ AC_DEFINE([HAVE_GETCWD_DYN], [1],
+ [Define to 1 if getcwd(NULL, 0) works])],
+ [AC_MSG_RESULT(no)])
+
 AC_LANG(C++)
 
 # Xerces is required
--- a/xsec/tools/checksig/checksig.cpp
+++ b/xsec/tools/checksig/checksig.cpp
@@ -57,7 +57,6 @@
 
 #if defined(HAVE_UNISTD_H)
 # include <unistd.h>
-# define _MAX_PATH PATH_MAX
 #else
 # if defined(HAVE_DIRECT_H)
 #  include <direct.h>
@@ -438,10 +437,14 @@ int evaluate(int argc, char ** argv) {
 		AnonymousResolver theAnonymousResolver;
 		     
 		// Map out base path of the file
-		char path[_MAX_PATH];
-		char baseURI[(_MAX_PATH * 2) + 10];
-		getcwd(path, _MAX_PATH);
-
+#if HAVE_GETCWD_DYN
+		char *path = getcwd(NULL, 0);
+		char *baseURI = (char*)malloc(strlen(path) + 8 + 1 + strlen(filename) + 1);
+#else
+		char path[PATH_MAX];
+		char baseURI[(PATH_MAX * 2) + 10];
+		getcwd(path, PATH_MAX);
+#endif
 		strcpy(baseURI, "file:///");		
 
 		// Ugly and nasty but quick
@@ -471,6 +474,10 @@ int evaluate(int argc, char ** argv) {
 		XMLCh * baseURIXMLCh = XMLString::transcode(baseURI);
 
 		XMLUri uri(MAKE_UNICODE_STRING(baseURI));
+#if HAVE_GETCWD_DYN
+		free(path);
+		free(baseURI);
+#endif
 
 		if (useAnonymousResolver == true) {
 			// AnonymousResolver takes precedence
--- a/xsec/tools/cipher/cipher.cpp
+++ b/xsec/tools/cipher/cipher.cpp
@@ -60,7 +60,6 @@
 
 #if defined(HAVE_UNISTD_H)
 # include <unistd.h>
-# define _MAX_PATH PATH_MAX
 #else
 # if defined(HAVE_DIRECT_H)
 #  include <direct.h>
@@ -639,10 +638,14 @@ int evaluate(int argc, char ** argv) {
 			if (useInteropResolver == true) {
 
 				// Map out base path of the file
-				char path[_MAX_PATH];
-				char baseURI[(_MAX_PATH * 2) + 10];
-				getcwd(path, _MAX_PATH);
-
+#if HAVE_GETCWD_DYN
+				char *path = getcwd(NULL, 0);
+				char *baseURI = (char*)malloc(strlen(path) + 8 + 1 + strlen(filename) + 1);
+#else
+				char path[PATH_MAX];
+				char baseURI[(PATH_MAX * 2) + 10];
+				getcwd(path, PATH_MAX);
+#endif
 				strcpy(baseURI, "file:///");		
 
 				// Ugly and nasty but quick
@@ -671,6 +674,10 @@ int evaluate(int argc, char ** argv) {
 				baseURI[lastSlash + 1] = '\0';
 
 				XMLCh * uriT = XMLString::transcode(baseURI);
+#if HAVE_GETCWD_DYN
+				free(path);
+				free(baseURI);
+#endif
 
 				XencInteropResolver ires(doc, &(uriT[8]));
 				XSEC_RELEASE_XMLCH(uriT);
--- a/xsec/tools/templatesign/templatesign.cpp
+++ b/xsec/tools/templatesign/templatesign.cpp
@@ -74,7 +74,6 @@
 
 #if defined(HAVE_UNISTD_H)
 # include <unistd.h>
-# define _MAX_PATH PATH_MAX
 #else
 # if defined(HAVE_DIRECT_H)
 #  include <direct.h>
@@ -1169,10 +1168,14 @@ int main(int argc, char **argv) {
      
 	// Map out base path of the file
 	char * filename=argv[argc-1];
-	char path[_MAX_PATH];
-	char baseURI[(_MAX_PATH * 2) + 10];
-	getcwd(path, _MAX_PATH);
-
+#if HAVE_GETCWD_DYN
+	char *path = getcwd(NULL, 0);
+	char *baseURI = (char*)malloc(strlen(path) + 8 + 1 + strlen(filename) + 1);
+#else
+	char path[PATH_MAX];
+	char baseURI[(PATH_MAX * 2) + 10];
+	getcwd(path, PATH_MAX);
+#endif
 	strcpy(baseURI, "file:///");		
 
 	// Ugly and nasty but quick
@@ -1202,6 +1205,10 @@ int main(int argc, char **argv) {
 
 	theResolver->setBaseURI(MAKE_UNICODE_STRING(baseURI));
 	sig->setURIResolver(theResolver);
+#if HAVE_GETCWD_DYN
+	free(path);
+	free(baseURI);
+#endif
 
 	try {
 		sig->load();
--- a/xsec/tools/txfmout/txfmout.cpp
+++ b/xsec/tools/txfmout/txfmout.cpp
@@ -57,7 +57,6 @@
 
 #if defined(HAVE_UNISTD_H)
 # include <unistd.h>
-# define _MAX_PATH PATH_MAX
 #else
 # if defined(HAVE_DIRECT_H)
 #  include <direct.h>
@@ -502,10 +501,14 @@ int main(int argc, char **argv) {
 		theResolver;
 		 
 	// Map out base path of the file
-	char path[_MAX_PATH];
-	char baseURI[(_MAX_PATH * 2) + 10];
-	getcwd(path, _MAX_PATH);
-
+#if HAVE_GETCWD_DYN
+	char *path = getcwd(NULL, 0);
+	char *baseURI = (char*)malloc(strlen(path) + 8 + 1 + strlen(filename) + 1);
+#else
+	char path[PATH_MAX];
+	char baseURI[(PATH_MAX * 2) + 10];
+	getcwd(path, PATH_MAX);
+#endif
 	strcpy(baseURI, "file:///");
 	strcat(baseURI, path);
 	strcat(baseURI, "/");
@@ -526,7 +529,10 @@ int main(int argc, char **argv) {
 	baseURI[lastSlash + 1] = '\0';
 
 	theResolver.setBaseURI(MAKE_UNICODE_STRING(baseURI));
-
+#if HAVE_GETCWD_DYN
+	free(path);
+	free(baseURI);
+#endif
 	sig->setURIResolver(&theResolver);
 
 

Reply via email to