opae-c: account for null char in str fn's
@@ -223,17 +223,23 @@ build_error_list(const char *path, struct error_list **list)
struct error_list **el = list;
errno_t err;
- if (len+1 > FILENAME_MAX) {
+ // add 3 to the len
+ // 1 for the '/' char
+ // 1 for the minimum length of a file appended
+ // 1 for null string to terminate
+ // if we go over now, then leave without doing anything else
+ if (len+3 > FILENAME_MAX) {
FPGA_MSG("path too long");
return 0;
}
- err = strncpy_s(basedir, FILENAME_MAX-1, path, FILENAME_MAX-1);
+ err = strncpy_s(basedir, FILENAME_MAX-3, path, len);
if (err != EOK) {
FPGA_MSG("strncpy_s() failed with return value %u", err);
return 0;
}
basedir[len++] = '/';
+ // now we've added one to length
dir = opendir(path);
if (!dir) {
@@ -264,8 +270,9 @@ build_error_list(const char *path, struct error_list **list)
}
// build absolute path
- // dmax (arg2) must be at least smax+1 (arg4)
- err = strncpy_s(basedir + len, FILENAME_MAX - len,
+ // dmax (arg2) is restricted max length of resulting dest,
+ // including null - it must also be at least smax+1 (arg4)
+ err = strncpy_s(basedir + len, FILENAME_MAX - len - 1,
de->d_name, subpath_len);
if (err != EOK) {
FPGA_MSG("strncpy_s() failed with return value %u", err);
