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

HTTP(socket)下载遇到valgrind提示的错误: Conditional jump or move depends on uninitialised value(s)的更多相关文章

  1. chrome下载提示网络错误

    问题背景:项目开发测试阶段出现该问题. 复现:开发调试过程中一直没有问题,本地下载excel.Word.pdf 都完美,但是在服务部署到服务器之后,测试环境的chrome就总是下载失败,提示网络错误. ...

  2. docker pull提示x509错误的对应方法

    在一台虚拟机上使用docker pull时出现了x509错误,相关原因与对应方法简单memo如下. 错误现象 在使用docker pull从dockerhub上下载镜像时提示如下错误 docker: ...

  3. vs2015启动网站调试提示 HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容。 解决方法

    今天安装了vs2015 下载一个项目进行试用,启动调试的时候提示 HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容. 最可能的原因: 没有为请求的 URL ...

  4. c# webapi发布到windows server 2008 r2 iis上提示404错误

    项目服务端是一组RestFul风格的webapi,发布到本机的iis没有问题,但是发布到服务器以后就提示404错误.本机是win10的,服务器是windows server 2008 R2 64位.之 ...

  5. 【我是老中医】VMware在win8.1下开Ubuntu提示”内部错误"解决方案

    这个题目起得很洋气啊,其实问题也比较好解决,但是我想多码几个字!!! 友情提示:如果不想看废话,请直接看最后的红字! 好的,咱们从头说(废话)起.话说我们学院每年都会组织大三的进行校企联合实训(其实就 ...

  6. 百度地图API提示"230错误 APP Scode校验失败"

    笔者近2天在 Android Studio上玩了一下百度地图,碰到了常见的"230错误 APP Scode校验失败",下面我来介绍一下具体的解决办法. 1.在andriodstud ...

  7. Windows 和 Linux下使用socket下载网页页面内容(可设置接收/发送超时)的代码

    主要难点在于设置recv()与send()的超时时间,具体要注意的事项,请看代码注释部分,下面是代码: #include <stdio.h> #include <sys/types. ...

  8. tomcat + jenkins启动tomcat后打开jenkins页面提示404错误的解决方案

    首先下载tomcat和jenkins,将下载的jenkins2.12 war放到tomcat的webapps文件夹下后执行/bin文件夹下的start启动后,打开http://localhost:80 ...

  9. 关于源码编译每次提示有错误 要make update-api

    最近编译newline的版本的时候..同事修改了andoid默认输入法为百度.这是系统自动提供的API,所以每次编译会提示 此时在编译源码生成SDK的过程中会出现这个问题:************** ...

随机推荐

  1. jsp的el表达式

    el表达式的英文(Expression Language) 1.访问 javabean,list,map,数组 2.可以进行一些运算 3.获得web开发的常用对象 导入jstl.jar和stander ...

  2. 初学CDQ分治-NEU1702

    关于CDQ分治,首先需要明白分治的复杂度. T(n) = 2T(n/2)+O(kn), T(n) = O(knlogn) T(n) = 2T(n/2)+O(knlogn), T(n) = O(knlo ...

  3. poj2208Pyramids(四面体面积--公式)

    链接 一公式题.. 证明讲解参照http://www.cnblogs.com/dgsrz/articles/2590309.html 注意对棱 顺序 #include <iostream> ...

  4. python使用open经常报错:TypeError: an integer is required的解决方案

    错误是由于从os模块引入了所有的函数导致的,os模块下有一个open函数,接受整型的文件描述符和打开模式,from os import *引入os模块的open函数,覆盖了python内建的open函 ...

  5. Javascript中日期函数的相关操作

    Date对象具有多种构造函数,下面简单列举如下: new Date() new Date(milliseconds) new Date(datestring) new Date(year, month ...

  6. Hibernate中的Configuration类

    Configuration类用来管理我们的配置文件的信息的,通过它,我们可以通过创建一个configuration实例来管理相应的配置文档,但是通常我们只创建一个configuration实例. 下面 ...

  7. Sqlserver_left join 、right join、 inner join 用法

    left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...

  8. C# Socket编程(3)编码和解码

    在网络通信中,很多情况下:比如说QQ聊天,通讯双方直接传递的都是字符信息.但是字符信息并不能够直接通过网络传输,这些字符集必须先转换成一个字节序列后才能够在网络中传输,于是这里就产生了编码和解码的概念 ...

  9. mysql: 两个字段合并,字符时间转时间戳,别名字段作为where条件查询

    有字段,a,b: a存的是:2016-10-10 b存的是:10:15:30 mysql将字段合并: concat(a, ' - ', b)  或者 concat(a, ' ', b) 字符时间转时间 ...

  10. [转]Android 使用Fragment界面向下跳转并一级级返回

      1.首先贴上项目结构图: 2.先添加一个接口文件BackHandledInterface.java,定义一个setSelectedFragment方法用于设置当前加载的Fragment在栈顶,主界 ...