shithub: rgbds

Download patch

ref: c6fb526a8887cd2226e2f46a15694802e4017b9e
parent: a3eb287044509203ecda26edad9c9c7d7651d0ca
author: bentley <[email protected]>
date: Sun Jan 17 11:26:46 EST 2010

if module name is too long, error out rather than silently truncate

--- a/src/lib/library.c
+++ b/src/lib/library.c
@@ -182,27 +182,14 @@
 	return (0);
 }
 
-void 
-TruncateFileName(char *dest, char *src)
-{
-	SLONG l;
-
-	l = strlen(src) - 1;
-	while ((l >= 0) && (src[l] != '\\') && (src[l] != '/'))
-		l -= 1;
-
-	strcpy(dest, &src[l + 1]);
-}
-
 sLibrary *
 lib_Find(sLibrary * lib, char *filename)
 {
-	char truncname[MAXNAMELENGTH];
+	if (strlen(filename) >= MAXNAMELENGTH)
+		errx(5, "Module name too long: %s", filename);
 
-	TruncateFileName(truncname, filename);
-
 	while (lib) {
-		if (strcmp(lib->tName, truncname) == 0)
+		if (strcmp(lib->tName, filename) == 0)
 			break;
 
 		lib = lib->pNext;
@@ -218,9 +205,9 @@
 
 	if ((f = fopen(filename, "rb"))) {
 		sLibrary *module;
-		char truncname[MAXNAMELENGTH];
 
-		TruncateFileName(truncname, filename);
+		if (strlen(filename) >= MAXNAMELENGTH)
+			errx(5, "Module name too long: %s", filename);
 
 		if ((module = lib_Find(lib, filename)) == NULL) {
 			module = malloc(sizeof *module);
@@ -235,7 +222,7 @@
 		}
 
 		module->nByteLength = file_Length(f);
-		strcpy(module->tName, truncname);
+		strcpy(module->tName, filename);
 		module->pData = malloc(module->nByteLength);
 		if (!module->pData)
 			errx(5, "Out of memory");
@@ -242,7 +229,7 @@
 
 		fread(module->pData, sizeof(UBYTE), module->nByteLength, f);
 
-		printf("Added module '%s'\n", truncname);
+		printf("Added module '%s'\n", filename);
 
 		fclose(f);
 	}
@@ -252,7 +239,6 @@
 sLibrary *
 lib_DeleteModule(sLibrary * lib, char *filename)
 {
-	char truncname[MAXNAMELENGTH];
 	sLibrary **pp, **first;
 	BBOOL found = 0;
 
@@ -259,9 +245,11 @@
 	pp = &lib;
 	first = pp;
 
-	TruncateFileName(truncname, filename);
+	if (strlen(filename) >= MAXNAMELENGTH)
+		errx(5, "Module name too long: %s", filename);
+
 	while ((*pp) && (!found)) {
-		if (strcmp((*pp)->tName, truncname) == 0) {
+		if (strcmp((*pp)->tName, filename) == 0) {
 			sLibrary *t;
 
 			t = *pp;
@@ -280,7 +268,7 @@
 	if (!found)
 		errx(5, "Module not found");
 	else
-		printf("Module '%s' deleted from library\n", truncname);
+		printf("Module '%s' deleted from library\n", filename);
 
 	return (*first);
 }