Skip to contentSkip to conversation

avoid trouble with failed strdup

PATCH Jim Meyering 1 participant 2 messages Jul 11, 2007 – Jul 13, 2007

lore
Share & tools
lore
#0 lore raw

Hi,

There are three uses of basename in the lvm2/cvs tree. The two uses in lvmcmdline.c operate on strdup'd copies, presumably to avoid the portability problem whereby an old basename implementation would modify its argument string. The third use (in tools/fsadm/fsadm.c) operates on a "const" string but has no such protection. Here's one of the uses from lvmcmdline.c:

       namebase = strdup(name);
       base = basename(namebase);

Note that if strdup fails, basename will dereference the NULL pointer. The same happens with the other use of strdup in that file.

The patch at the end of this message removes both strdup calls (and the related decl and free).

If you would like the code to continue to work even when linked with a buggy (er, ancient at least) basename implementation, here's a function you can use in place of basename.
Just change each of the three uses of "basename" to "last_component". If you're interested in using that, tell me where you'd like the new function, and I'll submit a patch with that addition. Hmm... I see there are no other uses of "bool", or inclusion of <stdbool.h>, so I'd also change it to use "int".

BTW, this last_component function is based on the GPL'd basename.c from gnulib. All of the ifdefs are to accommodate certain "other" types of systems.
=========================================================
#include <stdbool.h>

#ifndef DIRECTORY_SEPARATOR

# define DIRECTORY_SEPARATOR '/'
#endif

#ifndef ISSLASH

# define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
#endif

#ifndef FILE_SYSTEM_PREFIX_LEN

# if FILE_SYSTEM_ACCEPTS_DRIVE_LETTER_PREFIX
    /* This internal macro assumes ASCII, but all hosts that support drive
       letters use ASCII.  */
#  define _IS_DRIVE_LETTER(c) (((unsigned int) (c) | ('a' - 'A')) - 'a' \
				<= 'z' - 'a')
#  define FILE_SYSTEM_PREFIX_LEN(Filename) \
	   (_IS_DRIVE_LETTER ((Filename)[0]) && (Filename)[1] == ':' ? 2 : 0)
# else
#  define FILE_SYSTEM_PREFIX_LEN(Filename) 0
# endif
#endif

/* Return the address of the last file name component of NAME.
If NAME has no relative file name components because it is a file system root, return the empty string. */

char *
last_component (char const *name)
{
char const *base = name + FILE_SYSTEM_PREFIX_LEN (name); char const *p;
bool saw_slash = false;

while (ISSLASH (*base))

    base++;

for (p = base; *p; p++)

    {
      if (ISSLASH (*p))
	saw_slash = true;
      else if (saw_slash)
	{
	  base = p;
	  saw_slash = false;
	}
    }

return (char *) base;
}
=========================================================

Here's the patch.

Remove unchecked strdup that worked around buggy basename.
* tools/lvmcmdline.c (_find_command, lvm2_main): Let basename operate directly on its argument.

tools/lvmcmdline.c +4 -10
index 039eb51..7fbabb2 100644
@@ -479,18 +479,15 @@ void lvm_register_commands(void)
 static struct command *_find_command(const char *name)
 {
 	int i;
-	char *namebase, *base;
+	char *base;
 
-	namebase = strdup(name);
-	base = basename(namebase);
+	base = basename(name);
 
 	for (i = 0; i < _cmdline.num_commands; i++) {
 		if (!strcmp(base, _cmdline.commands[i].name))
 			break;
 	}
 
-	free(namebase);
-
 	if (i >= _cmdline.num_commands)
 		return 0;
 
@@ -1138,14 +1135,13 @@ static void _exec_lvm1_command(char **argv)
 
 int lvm2_main(int argc, char **argv, unsigned is_static)
 {
-	char *namebase, *base;
+	char *base;
 	int ret, alias = 0;
 	struct cmd_context *cmd;
 
 	_close_stray_fds();
 
-	namebase = strdup(argv[0]);
-	base = basename(namebase);
+	base = basename(argv[0]);
 	while (*base == '/')
 		base++;
 	if (strcmp(base, "lvm") && strcmp(base, "lvm.static") &&
@@ -1160,8 +1156,6 @@ int lvm2_main(int argc, char **argv, unsigned is_static)
 		unsetenv("LVM_DID_EXEC");
 	}
 
-	free(namebase);
-
 	if (!(cmd = init_lvm(is_static)))
 		return -1;
 
#1 lore raw
Quoted 9 lines · Jim Meyering

There are three uses of basename in the lvm2/cvs tree. The two uses in lvmcmdline.c operate on strdup'd copies, presumably to avoid the portability problem whereby an old basename implementation would modify its argument string. The third use (in tools/fsadm/fsadm.c) operates on a "const" string but has no such protection. Here's one of the uses from lvmcmdline.c:

       namebase = strdup(name);
       base = basename(namebase);

Alasdair and I talked about how to do this, and here's what I understood is desired:

The proposed log entry:

Eliminate uses of strdup+basename. Use last_component instead.
* tools/fsadm/fsadm.c: Include "last-component.h". (_usage): Use last_component, not basename.
* tools/lvmcmdline.c (_find_command, lvm2_main): Likewise.
* lib/misc/last-component.h: New file, derived from gnulib's basename.c
* include/.symlinks: Add lib/misc/last-component.h.
* WHATS_NEW: Mention this.

The corresponding patch:

WHATS_NEW +1 -0
index 56f579e..a35e005 100644
@@ -1,5 +1,6 @@
 Version 2.02.27 - 
 ================================
+  Eliminate uses of strdup+basename.  Use last_component instead.
   Fix configure libdevmapper.h check when --with-dmdir is used.
   Turn _add_pv_to_vg() into external library function add_pv_to_vg().
   Add pv_by_path() external library function.
include/.symlinks +1 -0
index 2fa755a..ccf6c34 100644
@@ -37,6 +37,7 @@
 ../lib/misc/configure.h
 ../lib/misc/crc.h
 ../lib/misc/intl.h
+../lib/misc/last-component.h
 ../lib/misc/lib.h
 ../lib/misc/lvm-exec.h
 ../lib/misc/lvm-file.h
lib/misc/last-component.h new file +38 -0
new file mode 100644
index 0000000..0618a17
@@ -0,0 +1,38 @@
+/* This function is derived from gnulib's basename.c */
+
+/*
+ * Copyright (C) 2007 Free Software Foundation, Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU General Public License v.2.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/* Return the address of the last file name component of NAME.  If
+   NAME has no relative file name components because it is a file
+   system root, return the empty string.  */
+
+static inline char *
+last_component(char const *name)
+{
+	char const *p;
+	int saw_slash = 0;
+
+	while (*name == '/')
+		name++;
+
+	for (p = name; *p; p++) {
+		if (*p == '/')
+			saw_slash = 1;
+		else if (saw_slash) {
+			name = p;
+			saw_slash = 0;
+		}
+	}
+
+	return (char *) name;
+}
tools/fsadm/fsadm.c +3 -1
index 61e428c..948b875 100644
@@ -32,6 +32,8 @@
 #include <sys/mount.h>
 #include <sys/vfs.h>
 
+#include "last-component.h"
+
 #define log_error(str, x...) fprintf(stderr, "%s(%u):  " str "\n", __FILE__, __LINE__, x)
 
 /* Filesystem related information */
@@ -45,7 +47,7 @@ struct fsinfo {
 static void _usage(const char *cmd)
 {
 	log_error("Usage: %s [check <filesystem> | resize <filesystem> <size>]",
-		  basename(cmd));
+		  last_component(cmd));
 }
 
 /* FIXME Make this more robust - /proc, multiple mounts, TMPDIR + security etc. */
tools/lvmcmdline.c +5 -10
index 039eb51..276a381 100644
@@ -20,6 +20,7 @@
 
 #include "stub.h"
 #include "lvm2cmd.h"
+#include "last-component.h"
 
 #include <signal.h>
 #include <syslog.h>
@@ -479,18 +480,15 @@ void lvm_register_commands(void)
 static struct command *_find_command(const char *name)
 {
 	int i;
-	char *namebase, *base;
+	char *base;
 
-	namebase = strdup(name);
-	base = basename(namebase);
+	base = last_component(name);
 
 	for (i = 0; i < _cmdline.num_commands; i++) {
 		if (!strcmp(base, _cmdline.commands[i].name))
 			break;
 	}
 
-	free(namebase);
-
 	if (i >= _cmdline.num_commands)
 		return 0;
 
@@ -1138,14 +1136,13 @@ static void _exec_lvm1_command(char **argv)
 
 int lvm2_main(int argc, char **argv, unsigned is_static)
 {
-	char *namebase, *base;
+	char *base;
 	int ret, alias = 0;
 	struct cmd_context *cmd;
 
 	_close_stray_fds();
 
-	namebase = strdup(argv[0]);
-	base = basename(namebase);
+	base = last_component(argv[0]);
 	while (*base == '/')
 		base++;
 	if (strcmp(base, "lvm") && strcmp(base, "lvm.static") &&
@@ -1160,8 +1157,6 @@ int lvm2_main(int argc, char **argv, unsigned is_static)
 		unsetenv("LVM_DID_EXEC");
 	}
 
-	free(namebase);
-
 	if (!(cmd = init_lvm(is_static)))
 		return -1;