0
#include "framework.h"
#include <stdio.h>
//-----------------------------------------------------------------------------
// Computes and returns the length of a string
//
// @param text string to check
//
// @return length of the string
//
int stringLength(char *text);

//-----------------------------------------------------------------------------
// Changes a string to upper case
//
// @param text string to modify
//
void toUpper(char *text);


//-----------------------------------------------------------------------------
int stringLength(char *text)
{
    int  string_length = 0;

    while (text[string_length] != '\000')
        string_length++;

    return string_length;
}

// Main Function
//
int main()
{
    char key[MAX_KEY_LENGTH] = {};
    int string_len = 0;

    do {
        printf(KEY_PROMPT);
        gets(key);
        string_len = stringLength(key);
        if (string_len <= MAX_KEY_LENGTH && string_len > 0)
        {
            break;
        }
    } while (string_len > MAX_KEY_LENGTH || string_len < 0);
}

This is my code. Always when i first put in a string thats too long (eg. "Student StudentStudent StudentStudent StudentStudent StudentStudent Student") and then one which is "correct" (eg. "Student Student")

it gives me this error: (look at picture)

error code

  • What is your `MAX_KEY_LENGTH`? – Shiv Nov 08 '20 at 12:35
  • 1
    Never use `gets`, it is no longer part of the standard C library. Please read [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) The first absurd entry `"Student StudentStudent StudentStudent StudentStudent StudentStudent Student"` breaks it. – Weather Vane Nov 08 '20 at 12:35
  • You have a stackoverflow, and it should not happen for such small string, is your code for a microcontroller ? – phoenixstudio Nov 08 '20 at 12:38
  • Hey, thanks for trying to help me first of all! –  Nov 08 '20 at 13:32
  • My MAX_KEY_LENGTH is 25 –  Nov 08 '20 at 13:32
  • What can i use instead @WeatherVane –  Nov 08 '20 at 13:32
  • @phoenixstudio not really, the final result should be a very simple encrypter and decrypter. (assignment for university) –  Nov 08 '20 at 13:33
  • You can use `fgets()` but remember it also contains the newline. – Weather Vane Nov 08 '20 at 13:37
  • Please post the contents of `framework.h` – user3629249 Nov 09 '20 at 03:40
  • regarding: `char key[MAX_KEY_LENGTH] = {};` This causes the compiler to output: *untitled1.c:35:32: warning: ISO C forbids empty initializer braces [-Wpedantic]* when compiling, always enable the warnings, then fix those warnings. ( for `gcc`, at a minimum use: `-Wall -Wextra -Wconversion -pedantic -std=gnu11` ) Note: other compilers use different options to produce the same results – user3629249 Nov 09 '20 at 03:46
  • the function: `stringlength()` is not needed. Rather call the `string.h` function: `strlen()` – user3629249 Nov 09 '20 at 03:48
  • OT: regarding: `void toUpper(char *text);` the header file: `ctype.h` contains the function: `toupper()` which would be a much better choice. Note: that function is not called anywhere in the posted code. – user3629249 Nov 09 '20 at 03:51
  • regarding; `printf(KEY_PROMPT);` What is `KEY_PROMPT`? – user3629249 Nov 09 '20 at 03:52
  • regarding: `while (string_len > MAX_KEY_LENGTH || string_len < 0);` the variable `string_len` can NEVER be less than 0 as the function: `string_length()` starts that value at 0 and counts upward – user3629249 Nov 09 '20 at 03:54
  • the whole `do...while` loop can be replaced with: `fgets( key, sizeof( key ), stdin ); key[ strcspn( key, "\n" ) } = '\0; size_t length = strlen( key );` – user3629249 Nov 09 '20 at 03:58

2 Answers2

0

why are you getting 'stack smashing'?

this statement:

gets(key);

keeps placing more and more characters into memory. so your input:

Student StudentStudent StudentStudent StudentStudent StudentStudent Student

overruns the input buffer: key[] The result is undefined behavior (and can lead to a seg fault event.) As you can see, the overrun of the input buffer key[] corrupted the stack. Going around the loop again does not 'magically' fix the corrupted stack.

Therefore, use fgets() rather than gets() As gets() does not know when to stop inputting more characters while fgets() is given a specific stop point.

Strongly suggest reading: man fgets for the details of that function

user3629249
  • 16,402
  • 1
  • 16
  • 17
0

I think you have buffer overflow errors. Check out this answer, hopefully this helps you.

naggab
  • 371
  • 4
  • 18