From c2498de5a1a00e7455f28e0744a5bc811eba54fa Mon Sep 17 00:00:00 2001 From: Lailouezzz Date: Thu, 2 Jan 2020 20:09:07 +0100 Subject: [PATCH] FIX : p7 list heap corruption + 0o display when dir --- lib/link/seven_fs/list.c | 24 +++++++++++----------- lib/link/seven_fs/topath.c | 34 +++++++++++++++++++------------ src/p7/main.c | 41 +++++++++++++++++++++++++++++--------- 3 files changed, 65 insertions(+), 34 deletions(-) diff --git a/lib/link/seven_fs/list.c b/lib/link/seven_fs/list.c index fed50be..2e4eacd 100644 --- a/lib/link/seven_fs/list.c +++ b/lib/link/seven_fs/list.c @@ -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; diff --git a/lib/link/seven_fs/topath.c b/lib/link/seven_fs/topath.c index 59e4498..083c7e8 100644 --- a/lib/link/seven_fs/topath.c +++ b/lib/link/seven_fs/topath.c @@ -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; diff --git a/src/p7/main.c b/src/p7/main.c index 1794a6b..cf9b92b 100644 --- a/src/p7/main.c +++ b/src/p7/main.c @@ -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: