[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[MiNT] MiNTlib getcwd problem & fix



Here's a fix to mintlib's getcwd function.

Essentially when asking for a very long filename then the dos2unx
function will quite happily convert the path, but wouldn't error on
longer paths than was possible in the provided buffer.

Alan.
Index: mintlib/getcwd.c
===================================================================
RCS file: /mint/mintlib/mintlib/getcwd.c,v
retrieving revision 1.6
diff -u -r1.6 getcwd.c
--- mintlib/getcwd.c	8 Oct 2003 15:23:14 -0000	1.6
+++ mintlib/getcwd.c	7 Jan 2008 14:26:12 -0000
@@ -69,6 +69,12 @@
 	else
 		/* convert DOS filename to unix */
 		_dos2unx(_path, buf, size);
+
+	if (errno == ENAMETOOLONG) {
+		if (buf_malloced)
+			free(buf);
+		return NULL;
+	}
 	
 	if (buf_malloced) {
 		size_t len = strlen (buf) + 1;
Index: mintlib/unx2dos.c
===================================================================
RCS file: /mint/mintlib/mintlib/unx2dos.c,v
retrieving revision 1.5
diff -u -r1.5 unx2dos.c
--- mintlib/unx2dos.c	8 Oct 2003 15:23:14 -0000	1.5
+++ mintlib/unx2dos.c	7 Jan 2008 14:26:12 -0000
@@ -1,5 +1,6 @@
 
 #include <ctype.h>
+#include <errno.h>
 #include <limits.h>
 #include <stdio.h>
 #include <string.h>
@@ -11,6 +12,8 @@
 
 /*
  * returns 0 for ordinary files, 1 for special files (like /dev/tty)
+ *
+ * or returns -1 on error, such as ENAMETOOLONG
  */
 
 int
@@ -101,6 +104,8 @@
 int
 _dos2unx(const char *dos, char *unx, size_t len)
 {
+	register int dos_length = strlen(dos);
+	register int count = 0;
 	register char c;
 
 	len--;			/* for terminating NUL */
@@ -133,14 +138,21 @@
 	/* convert slashes
 	 */
 	while ( (c = *dos++) != 0) {
+		count++;
 		if (c == '\\')
 			c = '/';
 		else if (__mint < 7)
 			c = tolower(c);
 		*unx++ = c;
 		len--;
-		if (len == 0)
+		if (len == 0) {
+			if (count < dos_length) {
+				__set_errno(ENAMETOOLONG);
+				*unx = 0;
+				return -1;
+			}
 			break;
+		}
 	}
 	*unx = 0;
 	return 0;