0

So I've used the following code a couple of times and always put it directly into the main function, which always worked flawlessly. But today I tried to pack it into a separate function. This is the function I wrote:

char *getexedir(char *pth)
{
    char path_save[512];
    char *p;
    char *exe_dir = malloc(512);
    if(exe_dir == NULL) {
        printf("Failed to allocate memory.\n");
        return(NULL);
    }

    if(!(p = strrchr(pth, '/'))) {
        if(getcwd(exe_dir, sizeof(exe_dir)) == NULL) {
            perror("Failed to get current cwd");
            return(NULL);
        }
    }
    else {
        *p = '\0';

        if(getcwd(path_save, sizeof(path_save)) == NULL) {
            perror("Failed to get current cwd");
            return(NULL);
        }

        if(chdir(pth) < 0) {
            perror("Failed to change directory");
            return(NULL);
        }

        if(getcwd(exe_dir, sizeof(exe_dir)) == NULL) {
            perror("Failed to get changed cwd");
            return(NULL);
        }

        if(chdir(path_save) < 0) {
            perror("Failed to change to previous directory");
            return(NULL);
        }
    }

    return(exe_dir);
}

Executing this code always failes at the second getcwd() with the error message "Numerical result out of range". Can somebody please explain the reason for that?

I am using Ubuntu 18.04, gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0

jww
  • 97,681
  • 90
  • 411
  • 885
skratpa
  • 130
  • 12
  • Hmmm, this doesn't look like the way to get the executable's directory, the *current* directory certainly can't be counted on. You should be able to open `/proc//exe` and get a symlink to the full path, then strip off the trailing name part. Where are you getting the `pth` parameter from? – Steve Friedl Nov 27 '19 at 00:27
  • 2
    `sizeof(exe_dir)` print that out. – kaylum Nov 27 '19 at 00:27
  • It's the first argument in the argv-array passed to main. – skratpa Nov 27 '19 at 00:28
  • sizeof(exe_dir) returns 8 bytes. Yep...found the problem. – skratpa Nov 27 '19 at 00:31
  • Yep, I tried it by creating exe_dir as a local variable and copied it into a second dynamic buffer, which is then returned. That worked. Damn a pointer is obviously most likely going to be 8 bytes on a 64x system. – skratpa Nov 27 '19 at 00:33
  • 2
    Any caller of `execvp` can put anything it likes in `argv[0]`; please make sure you're not relying on something unreliable. – Steve Friedl Nov 27 '19 at 00:35

0 Answers0