1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

libdm: check for _build_dev_path failure

Enhance internal function _build_dev_path for failure
if buffer would be too small.
Use memcpy instead printf for a single string.
This commit is contained in:
Zdenek Kabelac 2014-04-04 21:47:54 +02:00
parent 2c28197630
commit 6d3d2d5e3d
2 changed files with 20 additions and 9 deletions

View File

@ -1,5 +1,6 @@
Version 1.02.85 - Version 1.02.85 -
=================================== ===================================
Check for sprintf error when building internal device path.
Check for sprintf error when creating path for dm control node. Check for sprintf error when creating path for dm control node.
When buffer for dm_get_library_version() is too small, return error code. When buffer for dm_get_library_version() is too small, return error code.
Always reinitialize _name_mangling_mode in dm_lib_init(). Always reinitialize _name_mangling_mode in dm_lib_init().

View File

@ -192,13 +192,20 @@ void dm_log_init_verbose(int level)
_verbose = level; _verbose = level;
} }
static void _build_dev_path(char *buffer, size_t len, const char *dev_name) static int _build_dev_path(char *buffer, size_t len, const char *dev_name)
{ {
int r;
/* If there's a /, assume caller knows what they're doing */ /* If there's a /, assume caller knows what they're doing */
if (strchr(dev_name, '/')) if (strchr(dev_name, '/'))
snprintf(buffer, len, "%s", dev_name); r = dm_strncpy(buffer, dev_name, len);
else else
snprintf(buffer, len, "%s/%s", _dm_dir, dev_name); r = (dm_snprintf(buffer, len, "%s/%s",
_dm_dir, dev_name) < 0) ? 0 : 1;
if (!r)
log_error("Failed to build dev path for \"%s\".", dev_name);
return r;
} }
int dm_get_library_version(char *version, size_t size) int dm_get_library_version(char *version, size_t size)
@ -955,7 +962,8 @@ static int _add_dev_node(const char *dev_name, uint32_t major, uint32_t minor,
dev_t dev = MKDEV((dev_t)major, minor); dev_t dev = MKDEV((dev_t)major, minor);
mode_t old_mask; mode_t old_mask;
_build_dev_path(path, sizeof(path), dev_name); if (!_build_dev_path(path, sizeof(path), dev_name))
return_0;
if (stat(path, &info) >= 0) { if (stat(path, &info) >= 0) {
if (!S_ISBLK(info.st_mode)) { if (!S_ISBLK(info.st_mode)) {
@ -1005,8 +1013,8 @@ static int _rm_dev_node(const char *dev_name, int warn_if_udev_failed)
char path[PATH_MAX]; char path[PATH_MAX];
struct stat info; struct stat info;
_build_dev_path(path, sizeof(path), dev_name); if (!_build_dev_path(path, sizeof(path), dev_name))
return_0;
if (stat(path, &info) < 0) if (stat(path, &info) < 0)
return 1; return 1;
else if (_warn_if_op_needed(warn_if_udev_failed)) else if (_warn_if_op_needed(warn_if_udev_failed))
@ -1031,8 +1039,9 @@ static int _rename_dev_node(const char *old_name, const char *new_name,
char newpath[PATH_MAX]; char newpath[PATH_MAX];
struct stat info; struct stat info;
_build_dev_path(oldpath, sizeof(oldpath), old_name); if (!_build_dev_path(oldpath, sizeof(oldpath), old_name) ||
_build_dev_path(newpath, sizeof(newpath), new_name); !_build_dev_path(newpath, sizeof(newpath), new_name))
return_0;
if (stat(newpath, &info) == 0) { if (stat(newpath, &info) == 0) {
if (!S_ISBLK(info.st_mode)) { if (!S_ISBLK(info.st_mode)) {
@ -1106,7 +1115,8 @@ static int _open_dev_node(const char *dev_name)
int fd = -1; int fd = -1;
char path[PATH_MAX]; char path[PATH_MAX];
_build_dev_path(path, sizeof(path), dev_name); if (!_build_dev_path(path, sizeof(path), dev_name))
return fd;
if ((fd = open(path, O_RDONLY, 0)) < 0) if ((fd = open(path, O_RDONLY, 0)) < 0)
log_sys_error("open", path); log_sys_error("open", path);