FIX : p7 list heap corruption + 0o display when dir

This commit is contained in:
Lailouezzz 2020-01-02 20:09:07 +01:00
parent b1fa67b8ab
commit c2498de5a1
Signed by: Lailouezzz
GPG Key ID: 03FCE8A99EF8482C
3 changed files with 65 additions and 34 deletions

View File

@ -41,7 +41,7 @@ int CASIO_EXPORT casio_sevenfs_list(sevenfs_cookie_t *cookie, sevenfs_path_t *pa
/* send command packet */
msg((ll_info, "Sending the list command"));
if((err = casio_seven_send_cmdfls_reqallinfo(handle, dev))) {
msg((ll_fatal, "Couldn't send file transfer request/didn't receive answer"));
msg((ll_fatal, "Couldn't send file all info request/didn't receive answer"));
return (err);
} else if (response.casio_seven_packet_type == casio_seven_type_nak
&& response.casio_seven_packet_code == casio_seven_err_other) {
@ -88,31 +88,31 @@ int CASIO_EXPORT casio_sevenfs_list(sevenfs_cookie_t *cookie, sevenfs_path_t *pa
lfilename = filename ? strlen(filename) : 0;
if(dir && filename) {
/* Create node size 2 */
casio_make_pathnode(&fnode, 2);
casio_make_pathnode(&fnode->casio_pathnode_next, 1);
/* Create node */
casio_make_pathnode(&fnode, ldir);
casio_make_pathnode(&fnode->casio_pathnode_next, lfilename);
/* Dir node */
memcpy(&fnode->casio_pathnode_name, dir, ldir);
fnode->casio_pathnode_name[ldir] = '\0';
/* File node */
memcpy(&fnode->casio_pathnode_next->casio_pathnode_name, filename, lfilename);
fnode->casio_pathnode_next->casio_pathnode_name[lfilename] = '\0';
} else if(filename && !dir) {
/* Create node size 1 */
casio_make_pathnode(&fnode, 1);
/* Create node */
casio_make_pathnode(&fnode, lfilename);
/* File node */
memcpy(&fnode->casio_pathnode_name, filename, lfilename);
fnode->casio_pathnode_name[lfilename] = '\0';
} else if(dir && !filename) {
/* Create node size 1 */
casio_make_pathnode(&fnode, 1);
/* Create node */
casio_make_pathnode(&fnode, ldir);
/* Dir node */
memcpy(&fnode->casio_pathnode_name, dir, ldir);
fnode->casio_pathnode_name[ldir] = '\0';
}
fstat.casio_stat_size = fs;

View File

@ -47,11 +47,8 @@ int CASIO_EXPORT casio_make_sevenfs_path(sevenfs_cookie_t *cookie,
/* Get directory name and file name. */
if (!array->casio_path_nodes)
return (casio_error_invalid);
node = array->casio_path_nodes;
if (!node) return (casio_error_invalid);
if (node->casio_pathnode_next) {
if (node && node->casio_pathnode_next) {
dirsz = node->casio_pathnode_size + 1;
if (dirsz == 1 || dirsz > 13) return (casio_error_invalid);
dirname = (const char*)node->casio_pathnode_name;
@ -60,14 +57,19 @@ int CASIO_EXPORT casio_make_sevenfs_path(sevenfs_cookie_t *cookie,
dirname = NULL;
dirsz = 0;
}
if (node->casio_pathnode_next) {
if (node && node->casio_pathnode_next) {
/* too deep! */
return (casio_error_invalid);
}
filesz = node->casio_pathnode_size + 1;
if (filesz == 1 || filesz > 13) return (casio_error_invalid);
filename = (const char*)node->casio_pathnode_name;
if(node) {
filesz = node->casio_pathnode_size + 1;
if (filesz == 1 || filesz > 13) return (casio_error_invalid);
filename = (const char*)node->casio_pathnode_name;
} else {
filesz = 0;
filename = NULL;
}
/* Make the node. */
@ -83,12 +85,18 @@ int CASIO_EXPORT casio_make_sevenfs_path(sevenfs_cookie_t *cookie,
data[dirsz - 1] = 0;
path->sevenfs_path_dir = off;
data += dirsz; off += dirsz;
} else
} else {
path->sevenfs_path_dir = 0xFF;
memcpy(data, filename, filesz - 1);
data[filesz - 1] = 0;
path->sevenfs_path_file = off;
data += filesz; off += filesz;
}
if(filename) {
memcpy(data, filename, filesz - 1);
data[filesz - 1] = 0;
path->sevenfs_path_file = off;
data += filesz; off += filesz;
} else {
path->sevenfs_path_file = 0xFF;
}
memcpy(data, array->casio_path_device, 4);
data[4] = 0;
path->sevenfs_path_dev = off;

View File

@ -172,15 +172,38 @@ static void print_file_info(void *cookie,
memset(buf, ' ', sizeof(buf));
/* File into dir */
if (node->casio_pathnode_size >= 2) {
if (node->casio_pathnode_next) {
char *b = buf;
b += sprintf(b, "%s/", node->casio_pathnode_name);
b[sprintf(b, "%s", node->casio_pathnode_next->casio_pathnode_name)] = ' '; // replace '\0' by ' '
} else if (node->casio_pathnode_size == 1) { /* Juste one file or dir */
buf[sprintf(buf, (stat->casio_stat_type == CASIO_STAT_TYPE_DIR) ? "%s/" : "%s", node->casio_pathnode_name)] = ' '; // replace '\0' by ' '
/* Write dir */
memcpy(b, node->casio_pathnode_name, node->casio_pathnode_size);
b += node->casio_pathnode_size;
*b = '/';
b += 1;
node = node->casio_pathnode_next;
/* Write file */
memcpy(b, node->casio_pathnode_name, node->casio_pathnode_size);
b += node->casio_pathnode_size;
*b = ' '; // replace '\0' by ' '
b += 1;
/* Juste one file or dir */
} else if (node) {
char *b = buf;
/* Write */
memcpy(b, node->casio_pathnode_name, node->casio_pathnode_size);
b += node->casio_pathnode_size;
*b = stat->casio_stat_type == CASIO_STAT_TYPE_REG ? ' ' : '/' ; // replace '\0' by ' ' or '/' if it's a dir
b += 1;
}
/* Put the size if it's a regular file */
if (stat->casio_stat_type == CASIO_STAT_TYPE_REG) {
sprintf(&buf[28], "%10uo", (unsigned) stat->casio_stat_size);
}
/* Put the size */
sprintf(&buf[28], "%10uo", (unsigned) stat->casio_stat_size);
/* Put the string to stdout */
puts(buf);
@ -333,14 +356,14 @@ int main(int ac, char **av)
case mn_list:
// Initialize the path
path.casio_path_device = args.storage;
casio_make_pathnode(&path.casio_path_nodes, 1);
path.casio_path_nodes = NULL;
path.casio_path_flags = casio_pathflag_rel;
// Open 7.00 fs and list
if ((err = casio_open_seven_fs(&fs, handle))
|| (err = casio_list(fs, &path, print_file_info, NULL)))
break;
break;
case mn_optimize: