Subject: bin/28644: mount don't deal with trailing "/" well
To: None <gnats-admin@netbsd.org, netbsd-bugs@netbsd.org>
From: None <fab@gnux.info>
List: netbsd-bugs
Date: 12/13/2004 12:03:00
>Number:         28644
>Category:       bin
>Synopsis:       mount don't deal with trailing "/" well
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Dec 13 12:03:00 +0000 2004
>Originator:     Fabien Devaux
>Release:        2.0
>Organization:
>Environment:
>Description:
if the fstab contains directories like:
/mnt/cdrom
/mnt/removable/

and you type

$ mount /mnt/cdrom/
$ mount /mnt/removable

Then it don't work (the entry isn't matched) due to the trailing slash.
>How-To-Repeat:
try to mount passing a mountpoint with an appropriate "/" at end.
>Fix:
--- mount.c-orig        2004-12-10 18:39:09.000000000 +0100
+++ mount.c     2004-12-10 19:00:11.000000000 +0100
@@ -218,8 +218,26 @@
                        fstypename = mntbuf->f_fstypename;
                        mountopts  = NULL;
                } else {
+                       char *alternate_argv;
+                       size_t argv_len = strlen(*argv);
+
+                       if((*argv)[argv_len-1] == '/') {
+                               alternate_argv = strdup(*argv);
+                               alternate_argv[argv_len-1] = 0;
+                       } else {
+                               alternate_argv = malloc(argv_len+2);
+                               sprintf(alternate_argv, "%s/", *argv);
+                       }
+                       if(alternate_argv == NULL)
+                               errx(1, "Out of memory.");
+                       /*
+                        * try both with and without trailing slash
+                        * not depending on /etc/fstab notation
+                        */
                        if ((fs = getfsfile(*argv)) == NULL &&
                            (fs = getfsspec(*argv)) == NULL)
+                               if ((fs = getfsfile(alternate_argv)) == NULL &&
+                                               (fs = getfsspec(alternate_argv)) == NULL)
                                errx(1,
                                    "%s: unknown special file or file system.",
                                    *argv);
@@ -237,6 +255,7 @@
                        mntonname   = fs->fs_file;
                        fstypename  = fs->fs_vfstype;
                        mountopts   = fs->fs_mntops;
+                       free(alternate_argv);
                }
                rval = mountfs(fstypename, mntfromname,
                    mntonname, init_flags, options, mountopts, 0, NULL, 0);