Seg Fault using strcat and strcpy

1

Here's my code:

char CodificarCesar(char *texto_nao_codificado[256], int chave){

char alf[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\n','
char CodificarCesar(char *texto_nao_codificado[256], int chave){

char alf[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\n','%pre%'};
char mensagem_codificada[256], mensagem_original[256];

memset(mensagem_codificada,'%pre%',sizeof(mensagem_codificada));
memset(mensagem_original,'%pre%',sizeof(mensagem_original));
strcpy(mensagem_original,texto_nao_codificado);

for(int i=0;i<strlen(mensagem_original);i++)
    for(int j=0;j<strlen(alf);j++){
        if(mensagem_original[i]==alf[j]){
            mensagem_codificada[i]=alf[j+chave];
            break;
        }else if(mensagem_original[i]==toupper(alf[j])){
            mensagem_codificada[i]=toupper(alf[j+chave]);
            break;
        }else if(isspace(mensagem_original[i])){
            mensagem_codificada[i]=" ";
            break;
        }//else space
    }//for j

strcat(mensagem_codificada,"\n");
memset(mensagem_original,'%pre%',sizeof(mensagem_original));

return mensagem_codificada; }
'}; char mensagem_codificada[256], mensagem_original[256]; memset(mensagem_codificada,'%pre%',sizeof(mensagem_codificada)); memset(mensagem_original,'%pre%',sizeof(mensagem_original)); strcpy(mensagem_original,texto_nao_codificado); for(int i=0;i<strlen(mensagem_original);i++) for(int j=0;j<strlen(alf);j++){ if(mensagem_original[i]==alf[j]){ mensagem_codificada[i]=alf[j+chave]; break; }else if(mensagem_original[i]==toupper(alf[j])){ mensagem_codificada[i]=toupper(alf[j+chave]); break; }else if(isspace(mensagem_original[i])){ mensagem_codificada[i]=" "; break; }//else space }//for j strcat(mensagem_codificada,"\n"); memset(mensagem_original,'%pre%',sizeof(mensagem_original)); return mensagem_codificada; }

I'm doing a simple encryption program using cesar's cipher, however in two situations I'm getting segmentation faults in the beyond, apparently both cases showing that the strcat function is involved, and in none of them is it even called.

It's worth noting that in situations where I get segmentation failures are situations where strings with space are inputs.

In the first case it is in the function return, after all the string has been processed correctly.

In the second case it is in the processing of the strings, when the debugger arrives on the line that contains some call to the function strcpy (within the if's of equivalence with some character of the alphabet).

I could not find any similar case researching, nor could I have any idea of what causes or could be causing this. Any help would be welcome.

[EDIT] Thanks @Jefferson Quesado for warning that I was using strcpy for chars

[EDIT2] Targeting fails only with strings with spaces

    
asked by anonymous 09.09.2017 / 04:26

1 answer

1

Most of the changes I've made have been guided by compiler warnings, something we should never ignore.

I'll try to quote them so that the logic that guided the changes is clear:

  

warning: passing argument 2 of 'strcpy' from incompatible pointer type

That refers to this line:

strcpy(mensagem_original,texto_nao_codificado);

The problem here is that texto_nao_codificado was not declared with the right type, so both types do not play. texto_nao_codificado was declared as char *texto_nao_codificado[256] which is of type char** instead of char* or char[] as it was supposed.

  

assignment makes integer from pointer without a cast

In this line:

mensagem_codificada[i]=" ";

If mensagem_codificada is an array of char s then each position is a char logo can not be assigned to a position string but a char . Therefore " " should be ' '

  

function returns address of local variable

No return :

return mensagem_codificada;

Here the compiler indicates that we are not supposed to return local variable addresses because they are allocated in stack which is released after the function returns, causing access after the function to be invalid. Instead the variable must be allocated in the heap with malloc so that it persists after the function ends,

Soon the creation of this variable:

char mensagem_codificada[256];

It should become:

char* mensagem_codificada = malloc(256*sizeof(char));
  

return makes integer from pointer without a cast

The latter has yet to do with return and with the returned type does not play with the statement. You are returning char* when the statement:

char CodificarCesar(char texto_nao_codificado[256], int chave){

Indicates% with%. Just change the statement to indicate char also:

So:

char *CodificarCesar(char texto_nao_codificado[256], int chave){

There is yet another subtle problem that is when you exchange one letter for another, here:

mensagem_codificada[i]=alf[j+chave];

It may be the case that you pass the size of char* to an invalid memory location. For example, it has the letter alf and you want to move z , it will stay out of the array. To work around this you can use the 5 (modulo) operator with the size of the array to ensure that it goes around and stays within it:

mensagem_codificada[i]=alf[(j+chave)%26];

After making all changes, the code looks like this:

char* CodificarCesar(char texto_nao_codificado[256], int chave)
{

    char alf[]= {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\n','
strcpy(mensagem_original,texto_nao_codificado);
'}; char mensagem_original[256]; char* mensagem_codificada = malloc(256*sizeof(char)); memset(mensagem_codificada,'
mensagem_codificada[i]=" ";
',sizeof(mensagem_codificada)); memset(mensagem_original,'
return mensagem_codificada;
',sizeof(mensagem_original)); strcpy(mensagem_original,texto_nao_codificado); int i,j; for(i=0; i<strlen(mensagem_original); i++) for(j=0; j<strlen(alf); j++) { if(mensagem_original[i]==alf[j]) { mensagem_codificada[i]=alf[(j+chave)%26]; break; } else if(mensagem_original[i]==toupper(alf[j])) { mensagem_codificada[i]=toupper(alf[(j+chave)%26]); break; } else if(isspace(mensagem_original[i])) { mensagem_codificada[i]=' '; break; }//else space }//for j strcat(mensagem_codificada,"\n"); memset(mensagem_original,'
char mensagem_codificada[256];
',sizeof(mensagem_original)); return mensagem_codificada; } int main(){ char msg_orig[] = "abc"; printf("%s", msg_orig); //abc printf("\n%s",CodificarCesar(msg_orig, 2)); //cde return 0; }

See it working on Ideone

    
09.09.2017 / 12:12