as the problem i was having was assignment related and has been solved i've decided it remove the code. Thanks for the help.
-
Please create a [Short, Self Contained, Correct (Compilable), Example](http://www.sscce.org/) that can be used to reproduce your problem. – Lee Duhem Apr 20 '14 at 04:15
-
Edited. I've removed the struct as this is an assignment. – choczy Apr 20 '14 at 04:37
-
What is the sample input and expected output? – Lee Duhem Apr 20 '14 at 04:43
-
Note: Be kind: avoid long lines like `char password[6] = "Fedora";char account[15];int loop = 0;char directory[50];char struct1[20]; char struct2[20];char choice[1];account[0] = '\0';directory[0] = '\0';` – chux - Reinstate Monica Apr 20 '14 at 05:20
-
With account, inputting something as Fedora works, but with directory something like /home/Fedora is required. The second time calling directory should spit out: the directory x is being shared. – choczy Apr 20 '14 at 05:28
1 Answers
You have char choice[1];
but then you call fgetsTrim(choice, 2)
. That then calls fgets(choice, 2, stdin)
causing a buffer overflow (and thus undefined behaviour).
To fix this, make choice
bigger. However your code has a lot of magic numbers in it. Use sizeof
where possible, and use #define
constants in other places.
BTW you probably want to make it at least 3
. If it is size 2
, then fgets
will only extract the digit the person types, and not the newline. Then your next call to fgets
, wherever it may be, will pick up that newline. To be user-friendly you should probably use a fairly large buffer, there's no reason not to.
For example:
char choice[80];
// ...
fgetsTrim(choice, sizeof choice);
For the struct names:
#define STRUCT1_SIZE 20
char struct1[STRUCT1_SIZE] = { 0 }; // don't need to set it to zero later
// ... in function
fgetsTrim(struct1, STRUCT1_SIZE);
Another problem is char password[6] = "Fedora";
. This is NOT a string because it is not null-terminated. But you call login(password)
which then calls strncmp
, a function that expects a null-terminated string. So you have a read buffer overflow.
To fix this, write char password[] = "Fedora";
instead, then the compiler will pick the right size for you.
Edit: The createStruct
function has severe issues. It seems as if you think you can use the user's input as a typename... you can't. The line typedef struct fileProperties struct1;
means that the token struct1
in your code aliases that struct. Then your printf("%s", struct1...
will not compile because struct1
is now a type name.

- 138,810
- 21
- 208
- 365
-
Also, `strncmp(x, y, strlen(y))` is just the same as `memcmp(x, y, strlen(y))`. If they enter "Fedo" then it will report that the password is correct. You probably wanted `strcmp(password, input)` instead. – M.M Apr 20 '14 at 05:20
-
With password it should be char password[]; and then declare the value of password at a later date? and with choice, make it char choice[] as well? – choczy Apr 20 '14 at 05:26
-
Also "sizeof(password)" will that return the size of memory allocated to the char array password or the size of the string literal contained in it? – choczy Apr 20 '14 at 05:27
-
Edited to clarify. `sizeof password` gives the number of bytes allocated to `password`, naturally. If `password` is an array then that is the array size. Be careful when you are in a function like `createStruct`, because the function parameters are actually pointers (the `[]` syntax in the function header [is bogus](http://stackoverflow.com/questions/22677415/why-do-c-and-c-compilers-allow-array-lengths-in-function-signatures-when-they/22677793#22677793) ). Hence my suggestion to use defined sizes. – M.M Apr 20 '14 at 05:31
-
1Disagree about "`strncmp(x, y, strlen(y))` is just the same as `memcmp(x, y, strlen(y))`". If `strlen(y) - strlen(x) > 1`, `strncmp()` will behave well, but `memcmp()` will venture passed the end of `x`. ---Cancel : i take this back as the compare would have stop at the difference. Uncancel: `memcmp()` is not obliged to compare 1 `char` at a time, thus `memcmp()` could venture pass the end of `x` which is UB. – chux - Reinstate Monica Apr 20 '14 at 05:40
-
Fair enough, the `memcmp` version could read memory we don't own (which is UB of course) – M.M Apr 20 '14 at 05:47
-
With the recommended changes included, I'm still having a problem with using the directory function, after inputting a directory to share, going through the menu and calling it again causes strlen to return a 0. Even putting a strlen after writing to it the first time gives a length, somehow after going to the menu it becomes blank? – choczy Apr 20 '14 at 12:40
-
I don't know what you mean, there is no `strlen` in the directory function. Can you update your code and then post exactly what you are doing and what is happening, and what you expect to happen instead? – M.M Apr 20 '14 at 13:09
-
I'd use strlen as an error check to see how long the string was. I am trying to store a value in char directory, using the directory function. fgets correctly stores it in there, but calling the function again using the menu goes straight to entering another value, for some strange reason it works with the account function, where the second time, it executes the else statement. – choczy Apr 20 '14 at 16:15
-