我写了个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;
}

最新文章

  1. 几个有趣的WEB设备API 前端提高B格必备(一)——电池状态&amp;震动api
  2. MBTiles
  3. jQuery validation学习(2)验证身份证
  4. paip.解决 数据库mysql增加列 字段很慢添加字段很慢
  5. CI框架SESSION重写
  6. IOS开发之——类似微信摇一摇的功能实现
  7. Oracle小技巧
  8. HD2086A1 = ?
  9. 通过SCVMM分配iSCSI存储
  10. jtemplate使用笔记
  11. 【原】Spark Rpc通信源码分析
  12. Error In Work
  13. UESTC_秋实大哥与战争 2015 UESTC Training for Data Structures&lt;Problem D&gt;
  14. windows 下一个mysql password忘记改变
  15. oracle添加sequence
  16. html5中的postMessage解决跨域问题
  17. UVA 11255 Necklace
  18. js生成json数据
  19. or1200处理器的异常处理类指令介绍
  20. HtmlTestRunner无法生成HTML报告问题

热门文章

  1. ora-01033:oracle initialization or shutdown in progress 解决方法
  2. Android开发面试经——5.常见面试官提问Android题①
  3. IOS开发证书变成“此证书的签发者无效”解决方法
  4. 探索 Pexpect,第 1 部分:剖析 Pexpect
  5. jquery 设置checked=&quot;checked&quot;无效,radio未选中。。
  6. Unity3D在Windows的全屏和跨屏(双屏)方案
  7. SharpZipLib要支持unicode的文件名称
  8. Mybatis学习(壹)
  9. 使用XIB实现一个简单view
  10. C#读写EXCEL