zpool: import: use realloc for realloc, remove strtok

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: John Kennedy <john.kennedy@delphix.com>
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Closes #12094
This commit is contained in:
наб 2021-05-20 23:02:44 +02:00 committed by Brian Behlendorf
parent 31f4c8cb19
commit a281f7690d
3 changed files with 29 additions and 32 deletions

View File

@ -3499,16 +3499,8 @@ zpool_do_import(int argc, char **argv)
cachefile = optarg; cachefile = optarg;
break; break;
case 'd': case 'd':
if (searchdirs == NULL) { searchdirs = safe_realloc(searchdirs,
searchdirs = safe_malloc(sizeof (char *)); (nsearch + 1) * sizeof (char *));
} else {
char **tmp = safe_malloc((nsearch + 1) *
sizeof (char *));
bcopy(searchdirs, tmp, nsearch *
sizeof (char *));
free(searchdirs);
searchdirs = tmp;
}
searchdirs[nsearch++] = optarg; searchdirs[nsearch++] = optarg;
break; break;
case 'D': case 'D':
@ -3698,24 +3690,16 @@ zpool_do_import(int argc, char **argv)
* Check the environment for the preferred search path. * Check the environment for the preferred search path.
*/ */
if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) { if ((searchdirs == NULL) && (env = getenv("ZPOOL_IMPORT_PATH"))) {
char *dir; char *dir, *tmp = NULL;
envdup = strdup(env); envdup = strdup(env);
dir = strtok(envdup, ":"); for (dir = strtok_r(envdup, ":", &tmp);
while (dir != NULL) { dir != NULL;
if (searchdirs == NULL) { dir = strtok_r(NULL, ":", &tmp)) {
searchdirs = safe_malloc(sizeof (char *)); searchdirs = safe_realloc(searchdirs,
} else { (nsearch + 1) * sizeof (char *));
char **tmp = safe_malloc((nsearch + 1) *
sizeof (char *));
bcopy(searchdirs, tmp, nsearch *
sizeof (char *));
free(searchdirs);
searchdirs = tmp;
}
searchdirs[nsearch++] = dir; searchdirs[nsearch++] = dir;
dir = strtok(NULL, ":");
} }
} }
@ -3754,9 +3738,7 @@ zpool_do_import(int argc, char **argv)
} }
if (err == 1) { if (err == 1) {
if (searchdirs != NULL)
free(searchdirs); free(searchdirs);
if (envdup != NULL)
free(envdup); free(envdup);
nvlist_free(policy); nvlist_free(policy);
nvlist_free(pools); nvlist_free(pools);
@ -3795,9 +3777,7 @@ error:
nvlist_free(props); nvlist_free(props);
nvlist_free(pools); nvlist_free(pools);
nvlist_free(policy); nvlist_free(policy);
if (searchdirs != NULL)
free(searchdirs); free(searchdirs);
if (envdup != NULL)
free(envdup); free(envdup);
return (err ? 1 : 0); return (err ? 1 : 0);

View File

@ -49,6 +49,22 @@ safe_malloc(size_t size)
return (data); return (data);
} }
/*
* Utility function to guarantee realloc() success.
*/
void *
safe_realloc(void *from, size_t size)
{
void *data;
if ((data = realloc(from, size)) == NULL) {
(void) fprintf(stderr, "internal error: out of memory\n");
exit(1);
}
return (data);
}
/* /*
* Display an out of memory error message and abort the current program. * Display an out of memory error message and abort the current program.
*/ */

View File

@ -39,6 +39,7 @@ extern "C" {
* Basic utility functions * Basic utility functions
*/ */
void *safe_malloc(size_t); void *safe_malloc(size_t);
void *safe_realloc(void *, size_t);
void zpool_no_memory(void); void zpool_no_memory(void);
uint_t num_logs(nvlist_t *nv); uint_t num_logs(nvlist_t *nv);
uint64_t array64_max(uint64_t array[], unsigned int len); uint64_t array64_max(uint64_t array[], unsigned int len);