* Fix null terminated string in init_index
* Add check if out of memory
* Add debug message for file index.txt
* Add MAX_VIRT_FILES
This commit is contained in:
2023-11-20 16:36:09 +01:00
parent 38bdf73763
commit 65ad130c09

View File

@@ -10,6 +10,7 @@
#define VIRT_IMAGE_BMP 1
#define VIRT_IMAGE_GIF 2
#define VIRT_TEXT_TXT 3
#define MAX_VIRT_FILES 4
#define IMAGE_BUFFER_SIZE 81920
@@ -240,7 +241,7 @@ int tftp_write(void* handle, struct pbuf* p) {
void init_index(void) {
size_t len = 0;
// Add len of the virt files to the size
for (int i = 0; i < 2; i++) {
for (int i = 0; i < MAX_VIRT_FILES; i++) {
len += strlen(virt_file[i].name) + 1;
}
void* mem = NULL; // Pointer for internal use by the llfs library
@@ -249,10 +250,14 @@ void init_index(void) {
len += strlen(file->name) + 1;
}
len++; // +1 for the \0
virt_file[VIRT_INDEX_TXT].data = malloc(len);
virt_file[VIRT_INDEX_TXT].data = calloc(len, sizeof(char));
if (virt_file[VIRT_INDEX_TXT].data == NULL) {
LOG_FATAL(TAG, "Could not allocate memory for index.txt");
return;
}
virt_file[VIRT_INDEX_TXT].len = len;
for (int i = 0; i < 2; i++) {
for (int i = 0; i < MAX_VIRT_FILES; i++) {
str_cat_str(virt_file[VIRT_INDEX_TXT].data, len, virt_file[i].name);
str_cat(virt_file[VIRT_INDEX_TXT].data, len, '\n');
}
@@ -275,6 +280,7 @@ void tftp_server_init(void) {
LOG_INFO(TAG, "Initializing tftp server");
// init the index.txt virt_file
init_index();
LOG_DEBUG(TAG, "index.txt: %s", virt_file[VIRT_INDEX_TXT].data);
// init the virtImage.raw virt_file with 80kb of ram
virt_file[VIRT_IMAGE_BMP].data = calloc(IMAGE_BUFFER_SIZE, sizeof(char));
if (virt_file[VIRT_IMAGE_BMP].data == NULL) {