0

I have a function that begins like this:

int parseFile(char *filename, char *dirname) {

    int rv, roomStarter = 0;
    char buffer[100];
    FILE *fp;

    //char * slash = malloc(1 * sizeof(char));
    char * catfile = malloc(strlen(filename) + strlen(dirname) + 2); 
    strcpy(catfile,dirname);
    strcat(catfile,"/");
    strcat(catfile,filename);

    printf("file is %s and dirname is %s and size is %d catfile is %s \n",filename,dirname,strlen(dirname),catfile);
    fp = fopen(catfile, "r");

[snip]

The output is not what I expected, which is a qualified path to unk_room. I either get something like this:

$ ./prog

file is unk_room and dirname is ./dirname and size is 21

file is unk_room and dirname is ./dirname and size is 21 catfile is

Error opening file unk_room.

$

(Note, catfile is empty, and it tries to open filename)

or, errors about corrupt memory:

catfile is ./myprog.23202/
catfile is ./myprog.23202/gib_room
*** Error in `./myprog': double free or corruption (out): 0x0000000001f43000 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x81679)[0x7fa1eb7d7679]
/lib64/libc.so.6(+0xd6702)[0x7fa1eb82c702]
/lib64/libc.so.6(regfree+0x11)[0x7fa1eb837421]
./myprog[0x401ac0]
./myprog[0x401b90]
./myprog[0x401d3e]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7fa1eb778505]
./myprog[0x4010a9]
======= Memory map: ========
00400000-00403000 r-xp 00000000 00:32 3111810123                         /nfs/stak/users/smithj/CS344/hw2/myprog
00602000-00603000 r--p 00002000 00:32 3111810123                         /nfs/stak/users/smithj/CS344/hw2/myprog
00603000-00604000 rw-p 00003000 00:32 3111810123                         /nfs/stak/users/smithj/CS344/hw2/myprog
01f3a000-01f7c000 rw-p 00000000 00:00 0                                  [heap]
7fa1e4000000-7fa1e4021000 rw-p 00000000 00:00 0 
7fa1e4021000-7fa1e8000000 ---p 00000000 00:00 0 

What am I missing?

Lucky
  • 627
  • 5
  • 15
  • You forgot a `%s`: `catfile is \n` should be `catfile is %s\n` – Joseph Sible-Reinstate Monica May 07 '20 at 04:44
  • My bad, I missed that, updated the typo with debug info. – Lucky May 07 '20 at 05:07
  • 1
    `strlen` of "./dirname" must not be 21. So, it seems `dirname` argument ( And perhaps `filename`) contains some additional characters. Where and how do you do `free` on `catfile`? – HamidReza May 07 '20 at 05:54
  • I did it like this: free(catfile); at the end of the function. – Lucky May 07 '20 at 05:57
  • I bumped the malloc call to 100, but it makes no difference. :( – Lucky May 07 '20 at 06:01
  • `strlen` returns a `size_t` type and should have a `%zu` format specifier. Not sure that's causing all your problems, but it will be undefined behaviour, if `size_t` is bigger than an `int`. – Adrian Mole May 07 '20 at 06:34
  • 1
    I would suggest using `snprintf` here. It can write a string to a fixed buffer size, avoiding all kinds of copying and appending. `char catfile[PATH_MAX]; snprintf(catfile, sizeof catfile, "%s/%s", dirname, filename);` – Cheatah May 07 '20 at 06:46
  • @Cheatah That didn't work either. Here's what the output looked like: `dennisp.rooms.10634/gib_room dennisp.rooms.10L/baz_room dennisp.rooms.1PL/unk_room dennisp.rooms.1pL/fed_room dennisp.rooms.1�L/hip_room dennisp.rooms.1�L/zed_room dennisp.rooms.1�L/pug_room ` only the first iteration of the loop is correct. – Lucky May 07 '20 at 22:16
  • Then the problem is not here but somewhere else. All bets are off if you put garbage in `dirname` or `filename`. – Cheatah May 07 '20 at 23:18
  • @Cheatah I created dirname like this: `int pid = getpid(); /* this thing courtesy of https://stackoverflow.com/a/8257728 */ int pidsize = (int)((ceil(log10(pid))+1)*sizeof(char)); char dest[15]; char pidpart[pidsize]; sprintf(pidpart, "%d", pid); strcpy(dest, "dennisp.rooms."); strcat(dest, pidpart); if (mkdir(dest,0755) != 0) { printf("could not create directory, exiting."); exit(1); } ` No extra garbage discernable, and dir created fine. but...? – Lucky May 08 '20 at 19:16
  • Could you please tell me 3 things? How many character are there in `"dennisp.rooms."`? How many can be stored in `dest`? How many characters fit in `pidpart`? – Cheatah May 08 '20 at 22:43

0 Answers0