英文:
How can I fix this error in C using valgrind: invalid write of size 8
问题
After compiling my code, I've got some issues regarding the memory. After doing ./valgrind
on my Linux terminal, I've got the following error for this function:
> invalid write of size 8 at read_lines_from_file.
Here is my function read_lines_from_file
:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int lines_count = 0;
char** read_lines_from_file(FILE* file) {
fseek(file, 0, SEEK_SET);
char** lines = NULL;
char line[100];
while (fgets(line, 100, file)) {
line[strlen(line) - 1] = '#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int lines_count = 0;
char** read_lines_from_file(FILE* file) {
fseek(file, 0, SEEK_SET);
char** lines = NULL;
char line[100];
while (fgets(line, 100, file)) {
line[strlen(line) - 1] = '\0';
lines = realloc(lines, (lines_count + 1) * sizeof(char*));
lines[lines_count] = malloc(strlen(line) + 1);
strcpy(lines[lines_count], line);
lines_count++;
}
return lines;
}
';
lines = realloc(lines, (lines_count + 1) * sizeof(char*));
lines[lines_count] = malloc(strlen(line) + 1);
strcpy(lines[lines_count], line);
lines_count++;
}
return lines;
}
I don't understand where it comes from and how I can correct that to remove this error.
英文:
After compiling my code, I've got some issues regarding the memory. After doing ./valgrind on my Linux terminal, I've got the following error for this function:
> invalid write of size 8 at read_lines_from_file.
Here is my function read_lines_from_file
:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int lines_count = 0;
char** read_lines_from_file(FILE* file) {
fseek(file, 0, SEEK_SET);
char** lines = NULL;// ça renvoie NULL si le fichier est vide
char line[100];
while (fgets(line, 100, file)) {// nous faisons l'hypothèse qu'une ligne ne dépasse pas 100 caractères
line[strlen(line)-1]='#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
int lines_count = 0;
char** read_lines_from_file(FILE* file) {
fseek(file, 0, SEEK_SET);
char** lines = NULL;// ça renvoie NULL si le fichier est vide
char line[100];
while (fgets(line, 100, file)) {// nous faisons l'hypothèse qu'une ligne ne dépasse pas 100 caractères
line[strlen(line)-1]='\0';// on enlève le \n
lines = realloc(lines, (lines_count + 1) * sizeof(char*));
lines[lines_count] = malloc(strlen(line) + 1);
strcpy(lines[lines_count], line);
lines_count++;
}
return lines;
}
';// on enlève le \n
lines = realloc(lines, (lines_count + 1) * sizeof(char*));
lines[lines_count] = malloc(strlen(line) + 1);
strcpy(lines[lines_count], line);
lines_count++;
}
return lines;
}
I don't understand where it comes from and how I can correct that to remove this error.
答案1
得分: 2
存在以下问题:
- 不清楚是否解决了"在split处写入大小为1的无效写入"的问题。
- 调用者缺乏关于读取了多少行的信息。
- 在第2次调用
read_lines_from_file
时未重置为0,因此向调用者传递了错误的行数。 - 存在潜在的黑客攻击漏洞:当第一个字符是空字符时,
line[strlen(line)-1]='\0';
会导致未定义行为。而且,输入未必包括'\n'
。 - 缺乏错误检查:代码未检查分配错误。
- 无法很好处理长度超过100个字符的行。
英文:
Code has at least these problems:
Unclear if any solve "invalid write of size 1 at split".
Caller lacks info as to how many lines were read
read_lines_from_file
, on the 2nd call is not reset to 0 and so conveys to the caller the wrong number of lines.
Hacker exploit
line[strlen(line)-1]='\0';
is UB where the first character read is a null character.
Also, input does not certainly include a '\n'
.
// Better as
line[strcspn(line, "\n")] = ' // Better as
line[strcspn(line, "\n")] = '\0';
';
Lack of error checking
Code does not check for allocation errors.
Lines of 100+ characters are not well handled
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论