ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have attempted to use scanf as well; same problem.
I have used the following input:
Code:
Enter username: 123456
Enter password: 123456789
Code:
Enter username: harry
Enter password: sigfried
"Segmentation fault" immediately follows the second input and will follow the first if I comment out the second input.
I had a printf("debug") after the second fgets to help find where the issue was occurring, so I'm very confident it's there.
I recognise that the segfault is usually because the input to scanf/fgets has attempted to write outside the bounds of the array pointed to, yet I don't see how that's happening here.
Any help is greatly appreciated.
some notes:
I know return is superfluous in promptUser - I'll probably remove it
I know I encode then decode the same data - promptUser should only ever be used the first time this code is run (or if the username/password file is removed) so the performance hit shouldn't matter.
I've been plugging away at this problem and made some changes;
I have added:
strcspn() calls to clean up trailing \n before exiting promptUser
if(FilePointer == NULL) check - that was a derp - and some errno goodness.
Backtracing the segfault was basically unenlightening but I did find that the program had somehow moved to an fputs call before exiting the promptUser function ... very weird ... which is why I double-checked the FilePointer == NULL business.
Lo, and behold! There's a problem with permissions on the system.
I had assumed that this code wouldn't need any special attention to permissions as it's being run in a local directory by the same use as has compiled and run the code.
While pan64's suggestion to debug your code may be short, I do not think it was intended to be rude.
The example posted is complete and simple enough to yield to ordinary debugging methods. Try to isolate the line or function that is responsible for the segfault and look closely at the validity of your buffer pointers, their size and each use.
Quote:
Originally Posted by yakkmeister
Backtracing the segfault was basically unenlightening but I did find that the program had somehow moved to an fputs call before exiting the promptUser function ... very weird ... which is why I double-checked the FilePointer == NULL business.
That is not weird, it is however a big clue! Think about what you are seeing...
Quote:
Originally Posted by yakkmeister
Lo, and behold! There's a problem with permissions on the system.
I had assumed that this code wouldn't need any special attention to permissions as it's being run in a local directory by the same use as has compiled and run the code.
Please elaborate on that, what do you see that makes you suspect permission problems?
you need to compile/link your code with debug option (in general -g) and run it using a debugger, like ddd https://www.gnu.org/software/ddd/ (or command line version: gdb). You can inspect your variables, check the stack easily (and post related info if unclear).
from the other hand you may try valgrind too to catch such kind of issues.
you need to compile/link your code with debug option (in general -g) and run it using a debugger, like ddd https://www.gnu.org/software/ddd/ (or command line version: gdb). You can inspect your variables, check the stack easily (and post related info if unclear).
from the other hand you may try valgrind too to catch such kind of issues.
I guess you missed my previous response where I backtraced it?
I used gdb and checked all the frames.
The example posted is complete and simple enough to yield to ordinary debugging methods. Try to isolate the line or function that is responsible for the segfault and look closely at the validity of your buffer pointers, their size and each use.
Unless there's something there that I'm missing, they look fine ...
Quote:
Originally Posted by astrogeek
That is not weird, it is however a big clue! Think about what you are seeing...
It's not weird? Why? How can the program execute something it hasn't gotten to yet?
Quote:
Originally Posted by astrogeek
Please elaborate on that, what do you see that makes you suspect permission problems?
I used errno after an if(FilePointer == NULL) check.
I put that in the previous reply.
[EDIT]: Oh, I should add that the errno result literally said that the process did not have permission to access/create the file.
Since the file didn't exist and wasn't created, any call to fputs, etc, is going to definitely segfault.
Last edited by yakkmeister; 04-05-2017 at 04:40 AM.
Reason: adding relevant information
It's not weird? Why? How can the program execute something it hasn't gotten to yet?
Well of course it can't! That you thought that it did do so was the clue!
Quote:
Originally Posted by yakkmeister
I used errno after an if(FilePointer == NULL) check.
I put that in the previous reply.
[EDIT]: Oh, I should add that the errno result literally said that the process did not have permission to access/create the file.
Since the file didn't exist and wasn't created, any call to fputs, etc, is going to definitely segfault.
Yes, that would have been important to include when explaining the permission comment.
All's well that ends well, glad that you got it resolved!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.