C - is calling getcwd causing a bug here?
readlink
does not null-terminate the string it returns, so p2
may contain arbitrary garbage following /
.
You should do
ssize_t len = readlink(fdpath, p2, sizeof(p2));
if (len == -1) { /* error */ }
if (len == sizeof(p2)) { /* truncated */ }
p2[len] = '\0';
if (!stat(p2, &st2)) // ...
You should do error checking in many other places, too.
Adding and removing the unrelated code at the end probably changed the stack layout (because of the additional local variables), meaning that p2
contained different garbage with and without it. It could even be that in some cases, the garbage was null bytes, so that the string happened to be properly terminated after all. This sort of thing happens all the time with bugs in C code and isn't particularly meaningful; it's undefined behavior in the language of the C standard. Don't think too hard about why it happens; just try to find and fix the bug and don't write the same bug again :-)
Print getcwd path
strcmp returns an integer and not a boolean.
int strcmp (const char* str1, const char* str2);
It returns a 0 if the two strings are equal, so you should be checking the returned value in your if statement like this:
if(strcmp(argv[0],"pwd")==0)
getcwd error when $HOME
One suggested step in debugging was:
Since
getcwd()
setserrno
when it fails, you should probably reporterrno
, maybe withperror("getcwd")
. Although I'm not keen onperror()
, it is probably simplest here.
It turns out that the error set was EMFILE Too many open files.
So, now you know what the trouble is. The
getcwd()
is failing because you have opened a lot of files and not closed enough of them, and it needs some available file descriptors but you've not left it any that it can use.
And, when requested, I elaborated on that with:
You've opened files and/or directories (opening a directory with
opendir()
usually uses a file descriptor), and you've not closed them. Consequently, the system won't allow you to open any more files — and thegetcwd()
fails. It probably isn't immediate; your program has probably done some processing before that failure.
The OP observed:
I just saw that I haven't used
fclose
; give me a second and I will check it if that's it.
Making sure you've used fclose()
and closedir()
— and plain close()
if you've used any file descriptors by calling open()
directly — should help. If, however, the call to getcwd()
is the very first thing your code is doing, it won't be because you've opened many files (you haven't).
If there are still problems after files have been closed, then you need to take a step back and review the larger context.
For example:
- Why is
stat()
failing afterreaddir()
? stat()
error 'No such file or directory' when file name is returned byreaddir()
Related Topics
Signal Handling in Asm: Why am I Receiving Sigsegv When Invoking the Sys_Pause Syscall
Platform Independent Resource Management
What Does a Hexadecimal Number, with a Register in Parenthesis Mean in Assembly
Linux Kernel Device Driver to Dma from a Device into User-Space Memory
Linux Command Line Howto Accept Pairing for Bluetooth Device Without Pin
Iterate Over Lines Instead of Words in a for Loop of Shell Script
Why Disabling Interrupts Disables Kernel Preemption and How Spin Lock Disables Preemption
Gcc Compiled Binaries Give "Cannot Execute Binary File"
Profiling a (Possibly I/O-Bound) Process to Reduce Latency
Does Linux Malloc() Behave Differently on Arm Vs X86
How to Imshow with Invisible Figure in Matlab Running on Linux
Size() VS Ls -La VS Du -H Which One Is Correct Size
How to Filter Data Between 2 Dates with Awk in a Bash Script
Linux Why Can't I Pipe Find Result to Rm
Why Is Clock_Gettime So Erratic
Changing Location of Core Dump
Linux-Based Firmware, How to Implement a Good Way to Update
Provide Password to Ssh Command Inside Bash Script, Without The Usage of Public Keys and Expect