List command bug fix, easier debugging

- List command was generating bad data size because it was adding
directory entry char to buffer before testing buffer overflow.
- Added MAVLINK_FTP_DEBUG define to easily turn on/off noisier
debugging output
This commit is contained in:
Don Gagne 2014-08-12 14:23:26 -07:00
parent 62959d4d01
commit c8ecf59ab7
2 changed files with 41 additions and 21 deletions

View File

@ -39,6 +39,9 @@
#include "mavlink_ftp.h"
// Uncomment the line below to get better debug output. Never commit with this left on.
//#define MAVLINK_FTP_DEBUG
MavlinkFTP *MavlinkFTP::_server;
MavlinkFTP *
@ -125,7 +128,9 @@ MavlinkFTP::_worker(Request *req)
warnx("ftp: bad crc");
}
//printf("ftp: channel %u opc %u size %u offset %u\n", req->channel(), hdr->opcode, hdr->size, hdr->offset);
#ifdef MAVLINK_FTP_DEBUG
printf("ftp: channel %u opc %u size %u offset %u\n", req->channel(), hdr->opcode, hdr->size, hdr->offset);
#endif
switch (hdr->opcode) {
case kCmdNone:
@ -172,7 +177,9 @@ out:
// handle success vs. error
if (errorCode == kErrNone) {
hdr->opcode = kRspAck;
//warnx("FTP: ack\n");
#ifdef MAVLINK_FTP_DEBUG
warnx("FTP: ack\n");
#endif
} else {
warnx("FTP: nak %u", errorCode);
hdr->opcode = kRspNak;
@ -217,7 +224,9 @@ MavlinkFTP::_workList(Request *req)
return kErrNotDir;
}
//warnx("FTP: list %s offset %d", dirPath, hdr->offset);
#ifdef MAVLINK_FTP_DEBUG
warnx("FTP: list %s offset %d", dirPath, hdr->offset);
#endif
ErrorCode errorCode = kErrNone;
struct dirent entry, *result = nullptr;
@ -247,11 +256,13 @@ MavlinkFTP::_workList(Request *req)
uint32_t fileSize = 0;
char buf[256];
char direntType;
// store the type marker
// Determine the directory entry type
switch (entry.d_type) {
case DTYPE_FILE:
hdr->data[offset++] = kDirentFile;
// For files we get the file size as well
direntType = kDirentFile;
snprintf(buf, sizeof(buf), "%s/%s", dirPath, entry.d_name);
struct stat st;
if (stat(buf, &st) == 0) {
@ -259,29 +270,34 @@ MavlinkFTP::_workList(Request *req)
}
break;
case DTYPE_DIRECTORY:
hdr->data[offset++] = kDirentDir;
direntType = kDirentDir;
break;
default:
hdr->data[offset++] = kDirentUnknown;
direntType = kDirentUnknown;
break;
}
if (entry.d_type == DTYPE_FILE) {
// Files send filename and file length
snprintf(buf, sizeof(buf), "%s\t%d", entry.d_name, fileSize);
} else {
// Everything else just sends name
strncpy(buf, entry.d_name, sizeof(buf));
buf[sizeof(buf)-1] = 0;
}
size_t nameLen = strlen(buf);
// name too big to fit?
if ((nameLen + offset + 2) > kMaxDataLength) {
// Do we have room for the name, the one char directory identifier and the null terminator?
if ((offset + nameLen + 2) > kMaxDataLength) {
break;
}
// copy the name, which we know will fit
// Move the data into the buffer
hdr->data[offset++] = direntType;
strcpy((char *)&hdr->data[offset], buf);
//printf("FTP: list %s %s\n", dirPath, (char *)&hdr->data[offset-1]);
#ifdef MAVLINK_FTP_DEBUG
printf("FTP: list %s %s\n", dirPath, (char *)&hdr->data[offset-1]);
#endif
offset += nameLen + 1;
}
@ -342,7 +358,9 @@ MavlinkFTP::_workRead(Request *req)
}
// Seek to the specified position
//warnx("seek %d", hdr->offset);
#ifdef MAVLINK_FTP_DEBUG
warnx("seek %d", hdr->offset);
#endif
if (lseek(_session_fds[session_index], hdr->offset, SEEK_SET) < 0) {
// Unable to see to the specified location
warnx("seek fail");

View File

@ -154,15 +154,17 @@ private:
if (!success) {
warnx("FTP TX ERR");
}
// else {
// warnx("wrote: sys: %d, comp: %d, chan: %d, len: %d, checksum: %d",
// _mavlink->get_system_id(),
// _mavlink->get_component_id(),
// _mavlink->get_channel(),
// len,
// msg.checksum);
// }
}
#ifdef MAVLINK_FTP_DEBUG
else {
warnx("wrote: sys: %d, comp: %d, chan: %d, len: %d, checksum: %d",
_mavlink->get_system_id(),
_mavlink->get_component_id(),
_mavlink->get_channel(),
len,
msg.checksum);
}
#endif
}
uint8_t *rawData() { return &_message.data[0]; }