HTTP(socket)下载遇到valgrind提示的错误: Conditional jump or move depends on uninitialised value(s)
来源:互联网 发布:网络七层协议 详解 编辑:程序博客网 时间:2024/06/06 00:23
我写了个http下载函数,下载txt、jpg都正常,就是下载php有问题:valgrind会报错Conditional jump or move depends on uninitialised value(s),
函数定义:
char* httpdown(char *domain, int port, char *uri, int *length) ;
调用:
strcpy(uri, "/net.php");
rtn = httpdown("192.168.1.24", 80, uri, &len);
printf("File saved\n%s\n", rtn);
函数定义:
char* httpdown(char *domain, int port, char *uri, int *length) ;
调用:
strcpy(uri, "/net.php");
rtn = httpdown("192.168.1.24", 80, uri, &len);
printf("File saved\n%s\n", rtn);
大概发现printf这句会报那个错,如果uri是个txt或者jpg都不会报 。
经过一番折腾后发现是memcpy的问题,改成strncpy就可以了,但那样如果是图片又不能正确处理,所以,就只好判断下返回数据类型了,具体代码:
char* httpdown(char *domain, int port, char *uri, int *length) {char buffer[RECEIVE_BUF + 1];//memset(buffer, '\0', RECEIVE_BUF + 1);char* rtn;rtn = malloc(DOWNLOAD_BUF);rtn[0] = 0;(*length) = 0;int sockfd = openRequest(domain, port, uri, rtn, buffer);#ifdef DEBUGif (sockfd < 0){printf("Connect to %s:%d failed\n", domain, port);return rtn;}#elseif (sockfd < 0)return rtn;#endif#ifdef DEBUGprintf("Init connect to %s:%d done, fd=%d\n", domain, port, sockfd);#endifint len, needLen, dataLen=0;int current_buffer_size = DOWNLOAD_BUF;rtn[0] = 0;buffer[0] = 0;int type = -1;char * tmp;while (1) {len = recv(sockfd, buffer, RECEIVE_BUF, 0);if(type<0){//如果没有取得返回数据类型tmp = strstr(buffer, "Content-Type");//如果已经获得content-type——潜在bug如果content-type和他的值没有一起返回则会出问题,不管了if(tmp!=NULL){tmp = strstr(tmp, "text");if(tmp==NULL)type=1;elsetype=0;}}if (len > 0) {needLen = dataLen + len;while (current_buffer_size <= needLen) {current_buffer_size += DOWNLOAD_BUF;#ifdef DEBUGprintf("realloc to %d\n", current_buffer_size);#endifrtn = realloc(rtn, current_buffer_size);rtn[dataLen] = 0;}#ifdef DEBUGprintf("http sockect received %d data\n", len);#endifif(type<1)//字符串用strncatstrncat(rtn+dataLen, buffer, len);elsememcpy(rtn+dataLen, buffer, len);dataLen+=len;} else {if (len < 0) {if(errno>0)sprintf(rtn, "消息接收失败!错误代码是%d,错误信息是'%s'\n", errno,strerror(errno));return rtn;}#ifdef DEBUGprintf("Read data from %s:%d, complete\n", domain, port);#endifbreak;}}close(sockfd);(*length) = dataLen;return rtn;}
0 0
- HTTP(socket)下载遇到valgrind提示的错误: Conditional jump or move depends on uninitialised value(s)
- valgrind提示的错误:Conditional jump or move depends on uninitialised value(s)
- Conditional jump or move depends on uninitialised value(s)-1
- valgrind错误:Syscall param write(buf) points to uninitialised bytes(s)
- Spring的 depends-on
- depends-on的用法
- spring的depends-on属性
- VMWare遇到“Cannot open the disk 'XXX' or one of the snapshot disks it depends on. reason: failed to ”
- Android unspecified' depends on one or more Android Libraries but is a jar 的解决方法
- valgrind 常见错误提示信息
- Conditional row color based on a cell value
- Conditional Move Instructions(trap)
- spring的懒加载和depends-on
- Spring中depends-on的作用是什么?
- Spring中depends-on的作用是什么?
- Spring的depends-on属性Bean依赖
- Spring的depends-on属性Bean依赖
- Valgrind的下载安装
- HTTP 请求头中各类信息的具体含义
- VJ-detector: 级联检测器
- hdu2520我是菜鸟,我怕谁
- 深入理解JavaScript系列(26):设计模式之构造函数模式
- smack登陆
- HTTP(socket)下载遇到valgrind提示的错误: Conditional jump or move depends on uninitialised value(s)
- 阿里云Ubuntu部署java web - 目录
- 动态存储过程数据库
- fortify错误大全
- WebView的一个方法
- BroadcastReceiver应用详解
- sap修改系统标准表数据(保证找到LSETBF01;图讲解)
- openssl之RSA解码、验签
- N-Gram模型