Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.development.apps > #123

write function returning -1 in cookie_io_functions_t will crash the program

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.news-service.com!postnews.google.com!f9g2000vbz.googlegroups.com!not-for-mail
From Qian Xin <chianshin@gmail.com>
Newsgroups comp.os.linux.development.apps
Subject write function returning -1 in cookie_io_functions_t will crash the program
Date Fri, 27 May 2011 22:45:18 -0700 (PDT)
Organization http://groups.google.com
Lines 101
Message-ID <041b62d2-8168-4d5a-a9ee-e44ec5a58de0@f9g2000vbz.googlegroups.com> (permalink)
NNTP-Posting-Host 98.216.164.78
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
X-Trace posting.google.com 1306561518 4071 127.0.0.1 (28 May 2011 05:45:18 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Sat, 28 May 2011 05:45:18 +0000 (UTC)
Complaints-To groups-abuse@google.com
Injection-Info f9g2000vbz.googlegroups.com; posting-host=98.216.164.78; posting-account=h2aOXQoAAABUWj3vTBt6y6qfASmbbFYy
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-Header-Order HNKRUAELSC
X-HTTP-UserAgent Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.68 Safari/534.24,gzip(gfe)
Xref x330-a1.tempe.blueboxinc.net comp.os.linux.development.apps:123

Show key headers only | View raw


This program which is from
http://www.kernel.org/doc/man-pages/online/pages/man3/fopencookie.3.html
It also stated that if error happens, write should return -1;

But I found that returning -1 will crash the program. The reason is
explained in this bugzilla report.
http://sourceware.org/bugzilla/show_bug.cgi?id=2074

But glibc did fix the above bug.

Linux driver will also return negative value when error happens, Does
it have the same problem as the program here?


#define _GNU_SOURCE
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define INIT_BUF_SIZE 4

struct memfile_cookie {
   char   *buf;        /* Dynamically sized buffer for data */
   size_t  allocated;  /* Size of buf */
   size_t  endpos;     /* Number of characters in buf */
   off_t   offset;     /* Current file offset in buf */
};

ssize_t
memfile_write(void *c, const char *buf, size_t size)
{

   return -1;
}

ssize_t
memfile_read(void *c, char *buf, size_t size)
{
   return 0;
}

int
memfile_seek(void *c, off64_t *offset, int whence)
{
   return -1;
}

int
memfile_close(void *c)
{
   struct memfile_cookie *cookie = c;

   free(cookie->buf);
   cookie->allocated = 0;
   cookie->buf = NULL;

   return 0;
}

int
main(int argc, char *argv[])
{
   cookie_io_functions_t  memfile_func = {
       .read  = memfile_read,
       .write = memfile_write,
       .seek  = memfile_seek,
       .close = memfile_close
   };
   FILE *fp;
   struct memfile_cookie mycookie;
   ssize_t nread;
   long p;
   int j;
   enum CONST_HERE {BUFF_SIZE=9000};
   char buf[BUFF_SIZE]="goout\n";

     mycookie.buf = malloc(INIT_BUF_SIZE);
   if (mycookie.buf == NULL) {
       perror("malloc");
       exit(EXIT_FAILURE);
   }

   mycookie.allocated = INIT_BUF_SIZE;
   mycookie.offset = 0;
   mycookie.endpos = 0;

   fp = fopencookie(&mycookie,"w+", memfile_func);
   if (fp == NULL) {
       perror("fopencookie");
       exit(EXIT_FAILURE);
   }
   size_t out=fwrite(buf,BUFF_SIZE,1,fp);
   fprintf(stderr,"output-1 size:%d\n", out);
   out= fputs (buf,fp );
   fprintf(stderr,"output-2 size:%d\n", out);
   fflush(fp);

   exit(EXIT_SUCCESS);
}

Back to comp.os.linux.development.apps | Previous | NextNext in thread | Find similar


Thread

write function returning -1 in cookie_io_functions_t will crash the program Qian Xin <chianshin@gmail.com> - 2011-05-27 22:45 -0700
  Re: write function returning -1 in cookie_io_functions_t will crash the program David Schwartz <davids@webmaster.com> - 2011-06-03 04:27 -0700

csiph-web