HTTP(socket)下载遇到valgrind提示的错误: Conditional jump or move depends on uninitialised value(s)
2024-10-13 09:26:09
我写了个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);
大概发现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 DEBUG
if (sockfd < 0){
printf("Connect to %s:%d failed\n", domain, port);
return rtn;
}
#else
if (sockfd < 0)
return rtn;
#endif #ifdef DEBUG
printf("Init connect to %s:%d done, fd=%d\n", domain, port, sockfd);
#endif int 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;
else
type=0;
}
}
if (len > 0) {
needLen = dataLen + len;
while (current_buffer_size <= needLen) {
current_buffer_size += DOWNLOAD_BUF;
#ifdef DEBUG
printf("realloc to %d\n", current_buffer_size);
#endif
rtn = realloc(rtn, current_buffer_size);
rtn[dataLen] = 0;
}
#ifdef DEBUG
printf("http sockect received %d data\n", len);
#endif
if(type<1)//字符串用strncat
strncat(rtn+dataLen, buffer, len);
else
memcpy(rtn+dataLen, buffer, len);
dataLen+=len;
} else {
if (len < 0) {
if(errno>0)
sprintf(rtn, "消息接收失败!错误代码是%d,错误信息是'%s'\n", errno,
strerror(errno));
return rtn;
}
#ifdef DEBUG
printf("Read data from %s:%d, complete\n", domain, port);
#endif
break;
}
} close(sockfd);
(*length) = dataLen;
return rtn;
}
最新文章
- 几个有趣的WEB设备API 前端提高B格必备(一)——电池状态&;震动api
- MBTiles
- jQuery validation学习(2)验证身份证
- paip.解决 数据库mysql增加列 字段很慢添加字段很慢
- CI框架SESSION重写
- IOS开发之——类似微信摇一摇的功能实现
- Oracle小技巧
- HD2086A1 = ?
- 通过SCVMM分配iSCSI存储
- jtemplate使用笔记
- 【原】Spark Rpc通信源码分析
- Error In Work
- UESTC_秋实大哥与战争 2015 UESTC Training for Data Structures<;Problem D>;
- windows 下一个mysql password忘记改变
- oracle添加sequence
- html5中的postMessage解决跨域问题
- UVA 11255 Necklace
- js生成json数据
- or1200处理器的异常处理类指令介绍
- HtmlTestRunner无法生成HTML报告问题
热门文章
- ora-01033:oracle initialization or shutdown in progress 解决方法
- Android开发面试经——5.常见面试官提问Android题①
- IOS开发证书变成“此证书的签发者无效”解决方法
- 探索 Pexpect,第 1 部分:剖析 Pexpect
- jquery 设置checked=";checked";无效,radio未选中。。
- Unity3D在Windows的全屏和跨屏(双屏)方案
- SharpZipLib要支持unicode的文件名称
- Mybatis学习(壹)
- 使用XIB实现一个简单view
- C#读写EXCEL