libnilfs: nilfs_open: use last matched mount

In the current libnilfs implementation, nilfs_open() returns the first
matching mount when searching the mount table.  Therefore, when the
same mount points are stacked, the inaccessible mount point will be
unexpectedly selected.

Fix this issue by choosing the last match instead of the first one
when searching the mount table.

The changes required here also fixes potential memory leaks related to
device pathname and mount point pathname strings when nilfs_find_fs()
is called multiple times.

Signed-off-by: Ryusuke Konishi <konishi.ryusuke@gmail.com>
This commit is contained in:
Ryusuke Konishi 2024-02-28 03:04:21 +09:00
parent b5df575125
commit 87b1c80fd0

View File

@ -227,17 +227,23 @@ static int nilfs_find_fs(struct nilfs *nilfs, const char *dev, const char *dir,
}
if (has_mntopt(mntent[MNTFLD_OPTS], opt)) {
free(nilfs->n_dev);
nilfs->n_dev = strdup(mntent[MNTFLD_FS]);
if (unlikely(nilfs->n_dev == NULL))
if (unlikely(nilfs->n_dev == NULL)) {
free(nilfs->n_ioc);
nilfs->n_ioc = NULL;
ret = -1;
goto failed_proc_mounts;
}
free(nilfs->n_ioc);
nilfs->n_ioc = strdup(mntent[MNTFLD_DIR]);
if (unlikely(nilfs->n_ioc == NULL)) {
free(nilfs->n_dev);
nilfs->n_dev = NULL;
ret = -1;
goto failed_proc_mounts;
}
ret = 0;
break;
}
}
if (ret < 0)