struct tftp_rrq {
short opcode;
char *req;
};
struct tftp_conn{
int type;
int sock;
char *mode;
char *fname;
char *hostname;
struct sockaddr_in addr;
socklen_t addrlen;
FILE *fp;
int blocknr;
char msgbuf[512];
};
struct tftp_rrq* p_rrq = (struct tftp_rrq *)malloc(sizeof(struct tftp_rrq));
p_rrq->req = (char *)malloc(strlen(t->fname)+strlen(t->mode)+2);
p_rrq->opcode = htons(1);
memcpy(p_rrq->req, t->fname, strlen(t->fname));
memcpy(p_rrq->req + strlen(t->fname) + 1, t->mode,
strlen(t->mode));
其中t是struct tftp_conn *t。这个是没有问题的。
p_rrq->opcode = htons(1);
memcpy(p_rrq->req, t->fname, strlen(t->fname));
memcpy(p_rrq->req + strlen(t->fname) + 1, t->mode, strlen(t->mode));
在赋值的时候出的问题。上面三句都有问题,求大神
你req分配的空间大小是否正确。能否装下需要memcpy的数据
你这明显是内存越界的错误,就是你结构里的req指针并没有分配内存,这样你在使用的过程中其实用的就是指针本身的内存大小。
这里应该修改一下结构体
struct tftp_rrq
{
short opcode;
char req[2048]; // 给个足够使用的内存大小
};
解决了 应该是在给结构体申请完内存后,将其清零,再给里面的指针申请内存,再清零。就可以了。