Skip to content

std::bad_alloc in CoinFindDirSeparator() when current working directory doesn't exist #150

@cyderize

Description

@cyderize

On Linux (and possibly other platforms), when the current directory does not exist, then getcwd() always fails (and gives errno = ENOENT), meaning that the function enters an infinite loop trying to allocate larger and larger buffers until size overflows into a negative number leading to a std::bad_alloc.

inline char CoinFindDirSeparator()
{
int size = 1000;
char *buf = 0;
while (true) {
buf = new char[size];
if (getcwd(buf, size))
break;
delete[] buf;
buf = 0;
size = 2 * size;
}
// if first char is '/' then it's unix and the dirsep is '/'. otherwise we
// assume it's dos and the dirsep is '\'
char dirsep = buf[0] == '/' ? '/' : '\\';
delete[] buf;
return dirsep;
}

This causes Cbc and many other coin programs to crash on startup since they call CoinFindDirSeparator().

I'm not sure why the directory separator needs to be found in this way (and not just use defines to set it to backslash on Windows and forward slash everywhere else).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions