#Reached unreachable code

1 messages · Page 1 of 1 (latest)

rapid orchid
#

I'm attempting to experiment a little with the FS and ran into an issue I don't really understand.
In the first screenshot there are the 2 source files I made.
1.function gets the input from the user, 2.function is supposed to check if their was an error with the input and if the input is a valid path.
In the second screenshot you can see that the first iteration (which doesn't really handle the errors, just dummies them down to Error 1 & 2) works with a valid path and if I cheat and write Error the first condition "recognizes" that there was an error and classifies it as Error 1, but if I enter anything else it reaches unreachable code, which I don't get.

Shouldn't the catch part after openDirAbsolute catch the error and return Error 2 instead?

Does anyone understand whats happening here? I have changed it hundreds of times into try statements and making the return type a "![]const u8" and catching in the main function and so on, but never got anything out of it that explained to me whats going on.

sudden hound
#

openDirAbsolute asserts that you gave it an absolute path, it doesn't error if you give it a relative path

#

if you want to allow relative paths, use openDir

hasty ermine
#

The problem is that you are doing a UAF. (Use After Free.)
vars only exist for the duration of the scope, and then they are considered dead, and are reused by future var in the program.
Slices point to their data, which means that they need what they point to, to outlive your usage of them.
(buffer on L13 dies at L16, but input points into buffer.

#

In this particular case, the easiest would be to simply define that buffer as far up the call stack as you need it to live for; in this case, that'd be in main.