-2

So a while ago I had someone make me a tool in C that lets you randomize set-words with other random words that you can both write down yourself in the program. However after 20 entries the program just freezes and crashes. I only have basic knowledge in coding can't couldn't see what was causing it. Maybe a memory overflow of some kind?

Here is the code. I can't reach the guy who wrote it anymore so if someone could have a look I would greatly appreciate it a lot!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

#define BUFFER_SIZE 1000

void replaceAll(char *str, const char *oldWord, const char *newWord);


int main()
{
    int updates=0;
    char **upd_array ;
    printf("Enter number of words for replacements: ");
    scanf("%d", &updates);
    upd_array=(char**)malloc(updates+1*sizeof(char*));
    //upd_array = calloc(updates, sizeof *upd_array);
    int x=0;
    for(x=0 ; x<updates ; x++)
    {
        upd_array[x] = (char*)malloc(30*sizeof(char));
    }
    int i=0;
    for(i=0 ; i<updates ; i++)
    {
        printf("Enter word: \n");
        scanf("%s" ,upd_array[i]);
    }

    printf("list of word for updating");
    printf("\n");
     for(i=0 ; i<updates ; i++)
    {
        printf("%s",upd_array[i]);
        printf("\n");
    }





    int count=0;
    do{
    /* File pointer to hold reference of input file */
    FILE * fPtr;
    FILE * fTemp;
    char path[100];

    char buffer[BUFFER_SIZE];
    char oldWord[100], newWord[100];

    printf("Enter path of source file: ");
    scanf("%s", path);

    printf("Enter word to replace: ");
    scanf("%s", oldWord);

    /*  Open all required files */
    fPtr  = fopen(path, "r");
    fTemp = fopen("replace.tmp", "w");

    /* fopen() return NULL if unable to open file in given mode. */
    if (fPtr == NULL || fTemp == NULL)
    {
        /* Unable to open file hence exit */
        printf("\nUnable to open file.\n");
        printf("Please check whether file exists and you have read/write privilege.\n");
        exit(EXIT_SUCCESS);
    }
    /*
     * Read line from source file and write to destination
     * file after replacing given word.
     */
    while ((fgets(buffer, BUFFER_SIZE, fPtr)) != NULL)
    {
        // Replace all occurrence of word from current line
        int r = rand() % updates;
        strcpy(newWord , upd_array[r]);
        replaceAll(buffer, oldWord, newWord);

        // After replacing write it to temp file.
        fputs(buffer, fTemp);
    }

    /* Close all files to release resource */
    fclose(fPtr);
    fclose(fTemp);
    /* Delete original source file */
    remove(path);
    /* Rename temp file as original file */
    rename("replace.tmp", path);

    printf("\nSuccessfully replaced all occurrences of '%s'.\n", oldWord);
    printf("1 for continue 0 for terminate: ");
    scanf("%d", &count);
}while(count);

free(upd_array);
    return 0;
}

/**
 * Replace all occurrences of a given a word in string.
 */
void replaceAll(char *str, const char *oldWord, const char *newWord)
{
    char *pos, temp[BUFFER_SIZE];
    int index = 0;
    int owlen;
    //printf("in replace function");
    owlen = strlen(oldWord);


    /*
     * Repeat till all occurrences are replaced.
     */
    while ((pos = strstr(str, oldWord)) != NULL)
    {
        // Backup current line
        strcpy(temp, str);

        // Index of current found word
        index = pos - str;

        // Terminate str after word found index
        str[index] = '\0';

        // Concatenate str with new word
        strcat(str, newWord);

        // Concatenate str with remaining words after
        // oldword found index.
        strcat(str, temp + index + owlen);
    }
}

1 Answers1

0

Looks like the memory allocation has the issue.

  upd_array=(char**)malloc(updates+1*sizeof(char*));

should be

 upd_array= malloc( (updates+1)*sizeof(char*) );

or better,

 upd_array= malloc( (updates+1)* sizeof(*upd_array) );

or

 upd_array= malloc( (updates+1)* sizeof*upd_array );    

Then,

 scanf("%s" ,upd_array[i]);

should at least be

 scanf("%29s" ,upd_array[i]);   //stop buffer overflow

and you must check the return value for success.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Thanks! I got no more crashes there at all! Only now at the second stage (where it will actually look in a text file) when I write down a word it will freeze again. It worked with the first 7 words, now it freezes at the first one everytime. May I ask what you meant by checking the return value for success? Sorry if I'm being a bit vague, it's just that I'm not sure what would explain what's happening. – Niborius Jan 13 '21 at 11:44