cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Unable to send the image file.

From: Maxime Larocque <maxmtl2002_at_yahoo.ca>
Date: Tue, 16 Apr 2013 08:26:09 -0400

Hello Anil J,

Le 2013-04-16 07:17, Anil J a écrit :
> Can you please let me know what's wrong with the program I've written.

See my comments below...

>
> char *buffer;
> char buf[9];

buf[] should be at least 10 char long, you do a snprint(buf,10...)

> char contentLen[100];
>
> if (argc < 2) {
> printf("Not enough arumen spsecified. Please specify the URL\n");
> return 0;
> }
>
> /* Read the image file, and post it to the Servlet for processing. */
> fp = fopen(filename, "rb");
>
> if(fp == NULL) {
> printf("file not found!\n");
> } else {
> fseek(fp,0L,SEEK_END);
> fileLen = ftell(fp);
> printf("the file's length is %1d Bytes\n",fileLen);
> fclose(fp);

Why do you close the fp here? You need it later to actually read the
data. If the file is not found, you should abort the operation (do a
return).

> }
>
> /* Prepare the buffer to send to the server.*/
> buffer = (char *)malloc(fileLen + 1);
> if (!buffer) {
> fprintf(stderr, "Memory error!");
> fclose(fp);
> return;
> }
> //Read file contents into buffer
> fread(buffer, fileLen, 1, fp);
> fclose(fp);
> snprintf(buf, 10,"%d",fileLen);
> strcpy(contentLen, "Content-Length: ");
> strcpy(contentLen, buf);

Second strcpy: should be strcat. Or better yet, do a
snprintf(contentLen, sizeof(contentLen), "Content-Length: %d", fileLen).
It would replace both buf and strcpy.

Maxime

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2013-04-16