Check malloc return value and file name boundary
This commit is contained in:
parent
01d45535e3
commit
d34e598e28
|
@ -22,19 +22,22 @@ void
|
|||
basename(char dst[], const char src[])
|
||||
{
|
||||
int i;
|
||||
unsigned int len, offset;
|
||||
size_t len, offset;
|
||||
|
||||
len = strlen(src);
|
||||
|
||||
for (i = len - 1; i >= 0; i--) {
|
||||
offset = i + 1;
|
||||
if (src[i] == '/' && offset < len) {
|
||||
if (src[i] == '/' && offset < len && (len - offset) < 256) {
|
||||
strcpy(dst, src + offset);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
strcpy(dst, src);
|
||||
if (len < 256)
|
||||
strcpy(dst, src);
|
||||
else
|
||||
strcpy(dst, "");
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -61,6 +64,11 @@ copy_dir(const char path[])
|
|||
}
|
||||
|
||||
dirent = malloc(sizeof(BD_DIRENT));
|
||||
if (dirent == NULL) {
|
||||
fprintf(stderr, "Can't allocate memory for BD_DIRENT.\n");
|
||||
dir->close(dir);
|
||||
return 0;
|
||||
}
|
||||
|
||||
do {
|
||||
read = dir->read(dir, dirent);
|
||||
|
@ -71,6 +79,11 @@ copy_dir(const char path[])
|
|||
}
|
||||
|
||||
new_path = malloc(sizeof(char) * (strlen(path) + strlen(dirent->d_name) + 2));
|
||||
if (new_path == NULL) {
|
||||
fprintf(stderr, "Can't allocate memory for new_path.\n");
|
||||
all_good = 0;
|
||||
break;
|
||||
}
|
||||
strcpy(new_path, path);
|
||||
if (strcmp(path, "") != 0)
|
||||
strcat(new_path, "/");
|
||||
|
@ -139,11 +152,9 @@ copy_file(const char src[], const char dst[])
|
|||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int success;
|
||||
int success = 0;
|
||||
char dst[256];
|
||||
|
||||
success = 0;
|
||||
|
||||
if (argc == 2) {
|
||||
if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
|
||||
print_help();
|
||||
|
@ -164,6 +175,10 @@ main(int argc, char *argv[])
|
|||
break;
|
||||
case 4:
|
||||
basename(dst, argv[3]);
|
||||
if (!strcmp(dst, "")) {
|
||||
fprintf(stderr, "Invalid file name.\n");
|
||||
return 1;
|
||||
}
|
||||
success = copy_file(argv[3], dst);
|
||||
break;
|
||||
case 5:
|
||||
|
|
Loading…
Reference in New Issue