Fix -Werror=format-truncation on GCC 7

Fix formatting

Check snprintf return for error AND overflow
This commit is contained in:
Julien Lecoeur
2017-06-26 15:50:44 +02:00
committed by Julian Oes
parent 407b403e68
commit 7929287f73
6 changed files with 64 additions and 34 deletions
+23 -12
View File
@@ -355,20 +355,27 @@ MavlinkFTP::_workList(PayloadHeader *payload, bool list_hidden)
switch (result->d_type) {
#ifdef __PX4_NUTTX
case DTYPE_FILE:
case DTYPE_FILE: {
#else
case DT_REG:
#endif
// For files we get the file size as well
direntType = kDirentFile;
snprintf(buf, sizeof(buf), "%s/%s", dirPath, result->d_name);
struct stat st;
if (stat(buf, &st) == 0) {
fileSize = st.st_size;
case DT_REG: {
#endif
// For files we get the file size as well
direntType = kDirentFile;
int ret = snprintf(buf, sizeof(buf), "%s/%s", dirPath, result->d_name);
bool buf_is_ok = ((ret > 0) && (ret < sizeof(buf)));
if (buf_is_ok) {
struct stat st;
if (stat(buf, &st) == 0) {
fileSize = st.st_size;
}
}
break;
}
break;
#ifdef __PX4_NUTTX
case DTYPE_DIRECTORY:
@@ -397,7 +404,12 @@ MavlinkFTP::_workList(PayloadHeader *payload, bool list_hidden)
} else if (direntType == kDirentFile) {
// Files send filename and file length
snprintf(buf, sizeof(buf), "%s\t%d", result->d_name, fileSize);
int ret = snprintf(buf, sizeof(buf), "%s\t%d", result->d_name, fileSize);
bool buf_is_ok = ((ret > 0) && (ret < sizeof(buf)));
if (!buf_is_ok) {
buf[sizeof(buf) - 1] = '\0';
}
} else {
// Everything else just sends name
@@ -995,4 +1007,3 @@ void MavlinkFTP::send(const hrt_abstime t)
_reply(&ftp_msg);
} while (more_data);
}