Program hangs when calling read function

Program hangs when calling read function

Post by E_Hartwic » Sat, 21 Sep 2002 21:13:04



I'm trying to write a program that makes an data file, with records
(structs) and makes an index of them in a seperated file.
The first two function seem to work very well but the last function that
needs too read from the file and return a value from the first record
(dedicated for file info) then the whole progam hangs.

I've striped the function so it only try's to read the first record, and it
hangs on the funcion call read. Does anyone knows what is the error in this
code.

All function resides in unistd.h, it's the first time i'm programming under
Linux.

#include "idfile.h"

int         init_indexFile    (char* fName , size_t  size){
  rec newR;
  size_t length = sizeof(rec);
  int i;
  int filedescriptor, indexfile ;
  char datName[255], idxName[255] ;
  strcpy(datName, fName );
  strcat(datName, ".dat");
  strcpy(idxName, fName );
  strcat(idxName, ".idx");
  newR.key = size;
  newR.next = 1;
  strncpy(&(newR.data),"", DATAL);
  filedescriptor = open ( datName , O_WRONLY | O_CREAT , 0666);
  if (filedescriptor == -1) { perror("create"); return 0; }
  write(filedescriptor, &newR,length);                          
  for(i=0; i<=size; i++){
    if (i != size){ newR.next = i+1;} else newR.next=-1;
    if (write(filedescriptor, &newR, length) != length) { perror("write
1"); };
  }
  indexfile = open( idxName, O_WRONLY | O_CREAT , 0666 );
  close (filedescriptor);
  close (indexfile);

Quote:}

indexFile*  open_indexFile(char* fName){
  char idxName[255], datName[255];
  indexFile *index =  (indexFile*)malloc(sizeof(indexFile));
  strcpy(idxName, fName);
  strcat(idxName, ".idx");
  strcpy(datName, fName);
  strcat(datName, ".dat");
  if (index->idxfile = open (idxName, O_RDWR ) == -1){ perror("open
idxfile"); return NULL;}
  if (index->datfile = open (datName, O_RDWR ) == -1){ perror("open
datfile"); return NULL;}
  return index;
Quote:}

void  close_indexFile   (indexFile* iFile){
  close(iFile->idxfile);
  close(iFile->datfile);

Quote:}

unsigned    sizeOf_indexFile  (indexFile* iFile){
  rec *tmprec = (rec*)malloc(sizeof(rec));
  size_t length = sizeof(rec);
  //simple version only read first record no return to previous point
  lseek(iFile->datfile, 0,SEEK_SET );

/************************************
the program keeps* on the next instruction
*************************************/
  read(iFile->datfile, tmprec, length);
  return 0;

Quote:}

/*************************************
** and here a few lines from main.c **
**************************************/

int main(){
  indexFile *idx;
  init_indexFile    ("hlp",10000);
  idx = open_indexFile("hlp");
  if (idx == NULL ) printf("Nok/n");
  printf("%d\n", sizeOf_indexFile(idx));
  close_indexFile(idx);

Quote:}

 
 
 

Program hangs when calling read function

Post by Joshua Jone » Sat, 21 Sep 2002 22:24:33



>   char datName[255], idxName[255] ;
>   strcpy(datName, fName );
>   strcat(datName, ".dat");
>   strcpy(idxName, fName );
>   strcat(idxName, ".idx");

Use the 'n' versions of these functions... (strncpy, strncat) ... you
have a potential buffer overflow exploit here.

Quote:>     if (i != size){ newR.next = i+1;} else newR.next=-1;

You could probably make this more readable.

Quote:>   char idxName[255], datName[255];
>   indexFile *index =  (indexFile*)malloc(sizeof(indexFile));
>   strcpy(idxName, fName);
>   strcat(idxName, ".idx");
>   strcpy(datName, fName);
>   strcat(datName, ".dat");

Another potential buffer overflow.

Quote:>   if (index->idxfile = open (idxName, O_RDWR ) == -1){ perror("open
>   if (index->datfile = open (datName, O_RDWR ) == -1){ perror("open

This is definitely a problem.  It should be:

    if( (index->datfile = open(datName, O_RDWR)) == -1 ) { ... }

Fix this problem first.

--
 Joshua Jones
 josh(at)homemail.com  |  jonesjos(at)us.ibm.com

 
 
 

Program hangs when calling read function

Post by Kurtis D. Rade » Sun, 22 Sep 2002 01:40:13



>   if (index->idxfile = open (idxName, O_RDWR ) == -1){ perror("open
> idxfile"); return NULL;}
>   if (index->datfile = open (datName, O_RDWR ) == -1){ perror("open
> datfile"); return NULL;}

As Joshua pointed out this is where your problem lies, not with the read(2)
function. The reason the read(2) function is* is that it is attempting
to read from file descriptor zero (stdin) and hence your terminal. The reason
it is reading from file descriptor zero are the two statements above.

You need to grab your book on the C language and review the section that
discusses operator precedence. But I'll give you a hint. Note that zero
represents "false" in a boolean condition. Since the if() condition is false
(i.e., zero) the error handling blocks are not executed making it appear that
the opens were successful. Which they were. Because had the opens failed then
the if() condition would have been true. Need another hint? Consider the two
ways the if() condition statement could be parenthesized. Then consider that
the implied parenthesisation you expected is not the one the compiler used.

 
 
 

Program hangs when calling read function

Post by those who know me have no need of my nam » Sun, 22 Sep 2002 05:47:39


in comp.unix.programmer i read:


>>   char datName[255], idxName[255] ;
>>   strcpy(datName, fName );
>>   strcat(datName, ".dat");
>>   strcpy(idxName, fName );
>>   strcat(idxName, ".idx");

>Use the 'n' versions of these functions... (strncpy, strncat) ... you
>have a potential buffer overflow exploit here.

better: replace them with snprintf's.

--
bringing you boring signatures for 17 years

 
 
 

1. Question on writing c program implement cp function using UNIX system calls - read, write, etc.

I need to write a c program that will perform like the cp command in UNIX
and copy one file to another. I need to use the UNIX system calls read(),
write(), open(), close(). I know how to open the source file for read access
and destination file for write access with open(), but I am unsure of how to
use read() and write().

I know read takes my file descriptor (fd) of the source file I opened as its
1st arguement. For the second, I need to put those bytes in some sort of
buffer right? Could I just write them directly to the destination file
instead of storing into a buffer? Well if I have to make a buffer for input
I read in I do this:

c = (char *) calloc(100, sizeof(char));

Is that right? If I do this will it work for all files whether they are
binary or ascii files?

The 3rd arg of read() takes in how many  bytes to read, how do I tell it to
read in the entire source file?

So for read, this is what I have so far: read(fd, c, ????) where fd is my
source file descriptor and c as define above.

As for write, I have: write(fd2, ???, ???) where fd2 is the file descriptor
of my destination file. How do I specify it to write what I read from the
input source file and write all of those bytes inputted? Thanks for any
help.

2. ? Getting X working with onboard video?

3. how to know the instruction address of calling function within called function?

4. Initalising sound cardr Linux: Aztech Pro16c

5. Browser calls CGI C function which sets an env var and call a c function crashes

6. eth0 on Compaq Deskpro XL/Integrated NetFlex

7. Call a perl program and read the results back to/from a c program

8. Major frustration with PAM

9. How to get calling program name in called program

10. How to open/read a file during a function call in the networking kernel codes

11. C programming question: Calling a variadic function where the args come in via an array

12. The spawn function from /usr/bin/expect fails when called from "C" cgi program.

13. Call C function in Qt Program