c - 无法理解为什么我有这些错误:无效读取大小1和Syscall参数unlink(路径名)指向不可寻址的字节
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c - 无法理解为什么我有这些错误:无效读取大小1和Syscall参数unlink(路径名)指向不可寻址的字节相关的知识,希望对你有一定的参考价值。
我正在做一个练习,除此之外,我必须从txt文件中读取配置信息,并使用它们来设置服务器。我的代码似乎工作,服务器正常启动,各种缓冲区包含从文件中获取的信息,但启动我的程序与valgrind返回这些错误,我无法理解我错了什么。
这是主要的
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <config.h>
char* UNIXPATH1=NULL;
void cleanup(){
if(UNIXPATH1!=NULL)
unlink(UNIXPATH1);
}
int main(int argc, char *argv[]) {
FILE *fileconf=fopen("./try/in.txt","r");
if(fileconf==NULL){
fprintf(stderr, "File error
");
return -1;
}
set_conf(fileconf);
UNIXPATH1=malloc(strlen(returnUnixpath())*sizeof(char));
strncpy(UNIXPATH1,returnUnixpath(),strlen(returnUnixpath()));
cleanup();
atexit(cleanup);
FILE *fileout=fopen(UNIXPATH1,"w");
if(fileout==NULL){
fprintf(stderr, "File error
");
return -1;
}
fputs("hello",fileout);
fclose(fileout);
free(UNIXPATH1);
return 0;
}
这是config.h
#include <errno.h>
#include <stdlib.h>
#define MEM_ALL(r,c,msg)
if(!(r=c)) {perror(msg); fprintf(stderr, "ERROR: Out of memory
"); exit(errno); }
typedef int make_iso_compilers_happy;
char* UNIXPATH;
char* returnUnixpath(){
return UNIXPATH;
}
void set_conf(FILE *file){
char buffer[4096];
char* token1;
char* saveptr1;
char *array[3];
int i;
for(i=0;i<3;i++){
MEM_ALL(array[i],malloc(1*sizeof(char)),"malloc array[i]");
}
i=0;
char* tmp=malloc(sizeof(char));
while(fgets(buffer,4096,file)!=NULL){
if(buffer[0]!='#' && buffer[0]!='
' && buffer[0]!=' '){
buffer[strlen(buffer)-1]=' ';
token1=strtok_r(buffer," ",&saveptr1);
printf("s: %ld",strlen(token1));
tmp=realloc(tmp,(strlen(token1))*sizeof(char));
strncpy(tmp,token1,(strlen(token1)));
for(i=0;i<3;i++){
MEM_ALL(array[i],realloc(array[i],(strlen(token1)+1)*sizeof(char)),"realloc array[i]");
strncpy(array[i],token1,(strlen(token1)+1));
array[i][strlen(token1)]=' ';
if(i!=2)
token1=strtok_r(NULL," ",&saveptr1);
}
if(strncmp("UnixPath",tmp,8)==0){
MEM_ALL(UNIXPATH,malloc(strlen(array[2])*sizeof(char)),"malloc unixpath");
strncpy(UNIXPATH,array[2],strlen(array[2]));
}
}
}
free(tmp);
for(i=0;i<3;i++){
free(array[i]);
}
fclose(file);
}
这是in.txt文件
UnixPath = ./try/o.txt
这是valgrind输出
==5897== Invalid read of size 1
==5897== at 0x4C32D04: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5897== by 0x10905C: main (main.c:26)
==5897== Address 0x522e9bb is 0 bytes after a block of size 11 alloc'd
==5897== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5897== by 0x108EB7: set_conf (config.h:44)
==5897== by 0x10904A: main (main.c:25)
==5897==
==5897== Invalid read of size 1
==5897== at 0x4C32D04: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5897== by 0x10907D: main (main.c:27)
==5897== Address 0x522e9bb is 0 bytes after a block of size 11 alloc'd
==5897== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5897== by 0x108EB7: set_conf (config.h:44)
==5897== by 0x10904A: main (main.c:25)
==5897==
==5897== Syscall param unlink(pathname) points to unaddressable byte(s)
==5897== at 0x4F4DD47: unlink (syscall-template.S:78)
==5897== by 0x108FE3: cleanup (main.c:14)
==5897== by 0x1090AC: main (main.c:29)
==5897== Address 0x522ea0b is 0 bytes after a block of size 11 alloc'd
==5897== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5897== by 0x109064: main (main.c:26)
==5897==
==5897== Syscall param openat(filename) points to unaddressable byte(s)
==5897== at 0x4F4BC8E: open (open64.c:47)
==5897== by 0x4EC8589: _IO_file_open (fileops.c:189)
==5897== by 0x4EC8589: _IO_file_fopen@@GLIBC_2.2.5 (fileops.c:281)
==5897== by 0x4EBAEA9: __fopen_internal (iofopen.c:78)
==5897== by 0x4EBAEA9: fopen@@GLIBC_2.2.5 (iofopen.c:89)
==5897== by 0x1090CE: main (main.c:32)
==5897== Address 0x522ea0b is 0 bytes after a block of size 11 alloc'd
==5897== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5897== by 0x109064: main (main.c:26)
==5897==
==5897== Syscall param unlink(pathname) points to unaddressable byte(s)
==5897== at 0x4F4DD47: unlink (syscall-template.S:78)
==5897== by 0x108FE3: cleanup (main.c:14)
==5897== by 0x4E7F040: __run_exit_handlers (exit.c:108)
==5897== by 0x4E7F139: exit (exit.c:139)
==5897== by 0x4E5DB9D: (below main) (libc-start.c:344)
==5897== Address 0x522ea00 is 0 bytes inside a block of size 11 free'd
==5897== at 0x4C30D3B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5897== by 0x109138: main (main.c:40)
==5897== Block was alloc'd at
==5897== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5897== by 0x109064: main (main.c:26)
==5897==
s: 8==5897==
==5897== HEAP SUMMARY:
==5897== in use at exit: 11 bytes in 1 blocks
==5897== total heap usage: 15 allocs, 14 frees, 10,377 bytes allocated
==5897==
==5897== LEAK SUMMARY:
==5897== definitely lost: 0 bytes in 0 blocks
==5897== indirectly lost: 0 bytes in 0 blocks
==5897== possibly lost: 0 bytes in 0 blocks
==5897== still reachable: 11 bytes in 1 blocks
==5897== suppressed: 0 bytes in 0 blocks
==5897== Rerun with --leak-check=full to see details of leaked memory
答案
使用strncpy替换所有表单
UNIXPATH1=malloc(strlen(returnUnixpath())*sizeof(char));
strncpy(UNIXPATH1,returnUnixpath(),strlen(returnUnixpath()));
通过使用strdup
UNIXPATH1=strdup(returnUnixpath());
正如你所看到的,这是非常实用的,这也解决了你的案例中最后一个字符丢失的问题。当然删除array[i][strlen(token1)]=' ';
现在没用。
替换表格如:
tmp=realloc(tmp,(strlen(token1))*sizeof(char));
strncpy(tmp,token1,(strlen(token1)));
通过
tmp=realloc(tmp, strlen(token1) + 1);
strcpy(tmp,token1);
由于atexit(cleanup);
之后的free(UNIXPATH1);
,你需要在主要执行结束时在主要的else清理访问释放的字符串中执行UNIXPATH1 = NULL;
。
其他评论:
- 根据定义,
sizeof(char)
是1 - 将代码放在头文件中是很奇怪的,如果你在几个文件中剪切代码使用多个源文件
- 当你
#include
你的一个标题使用"file.h"
而不是<file.h>
在更改之后,valgrind下的执行不会发出无效访问的信号
以上是关于c - 无法理解为什么我有这些错误:无效读取大小1和Syscall参数unlink(路径名)指向不可寻址的字节的主要内容,如果未能解决你的问题,请参考以下文章