1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2025-01-20 09:32:07 +01:00

Fix potential buffer overflows in text back end

This commit is contained in:
q-posev 2022-07-08 12:26:23 +02:00
parent 1bc4fe44e3
commit 3cd7cac8f1

View File

@ -420,13 +420,14 @@ trexio_text_read_$group$ (trexio_text_t* const file)
/* conventional fcanf with "%s" only return the string before the first space character /* conventional fcanf with "%s" only return the string before the first space character
* to read string with spaces use "%[^\n]" possible with space before or after, i.e. " %[^\n]" * to read string with spaces use "%[^\n]" possible with space before or after, i.e. " %[^\n]"
*/ */
rc = fscanf(f, " %1023[^\n]", tmp_$group_dset$); rc = fscanf(f, " %1023[^\n]", buffer);
if (rc != 1) { if (rc != 1) {
trexio_text_free_read_$group$(buffer, f, file, $group$); trexio_text_free_read_$group$(buffer, f, file, $group$);
return NULL; return NULL;
} }
size_t tmp_$group_dset$_len = strlen($group$->$group_dset$[i]); size_t tmp_$group_dset$_len = strlen(buffer);
strncpy(tmp_$group_dset$, buffer, tmp_$group_dset$_len + 1);
tmp_$group_dset$ += tmp_$group_dset$_len + 1; tmp_$group_dset$ += tmp_$group_dset$_len + 1;
} }
} }
@ -484,14 +485,15 @@ trexio_text_read_$group$ (trexio_text_t* const file)
return NULL; return NULL;
} }
rc = fscanf(f, " %1023[^\n]", $group$->$group_str$); rc = fscanf(f, " %1023[^\n]", buffer);
if (rc != 1) { if (rc != 1) {
trexio_text_free_read_$group$(buffer, f, file, $group$); trexio_text_free_read_$group$(buffer, f, file, $group$);
return NULL; return NULL;
} }
/* Safer string conversion to avoid buffer overflow in fscanf */
strncpy($group$->$group_str$, buffer, $group$->len_$group_str$);
} }
// END REPEAT GROUP_ATTR_STR // END REPEAT GROUP_ATTR_STR
} else { } else {
continue; continue;