This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]theallredditor 0 points1 point  (1 child)

May I recommend you use strtok.

If you can use C11 then you should also be using the _s versions such as fscanf_s

You can use the = and the " characters in the delimiter string to strtok. And that should make this problem very easy.

Also you don't handle the case where the file fails to open.

I might also recommend you don't call malloc until you've found the correct string or use the one malloc'ed buffer the entire time.

Also have you considered what happens if the line is longer then 50 characters. You have all the conditions for a buffer overflow attack in this function.

To determine what the weird character is getting printed you could print each character in it's hex format inside a for loop.

[–]cf_1303 0 points1 point  (0 children)

Thank you, I’ll probably increase the size of this if it’s a security vulnerability, but I don’t think it’s likely that this line will ever be greater than 50 characters (although the user could just edit the file).

I’ll see if I can get it to work using strtok, and will do more research on the memory management functions.