1
This is 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','\0'};
char mensagem_codificada[256], mensagem_original[256];
memset(mensagem_codificada,'\0',sizeof(mensagem_codificada));
memset(mensagem_original,'\0',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,'\0',sizeof(mensagem_original));
return mensagem_codificada; }
I am doing a simple encryption program using César cipher, but in two situations I am receiving Segmentation faults from the beyond, apparently both cases showing that the strcat function is involved and in none of them it is even called.
Note that in situations where I get segmentation failure are situations where strings with space are input.
In the first case it is in the return of the function, after all the strings have been processed correctly.
In the second case it is in the string processing, when the Debugger arrives at the line containing some call to the strcpy function (within the equivalence if’s with some character of the alphabet).
I couldn’t find any similar case by researching, nor could I get any idea what causes or may be causing it.
Any help would be welcome.
[EDIT] Thanks @Jefferson Quesado for letting you know that I was using strcpy
chars
[EDIT2] Segmentation Failure only occurs with strings with spaces
You make a call from
strcpy
to copy a single character?strcpy(mensagem_codificada[i],alf[j+chave]);
?– Jefferson Quesado
Yeah, reviewing my code I was finding it weird to do
mensagem_codificada[i] = alf[j+chave]
and switched to `strcpy , even if it was working, to see if this was not the error, but doing so only created another error.– Miguel N.
I figured the segmentation flaw came from that call, but it already existed without doing it, right?
– Jefferson Quesado
Yes, it already existed, so much so that this was a solution kick that only generated a second segmentation failure separate from the first.
– Miguel N.
I found a possible bug/latent bug: if I pass string
"zorro"
with cipher key 2, which should be the result?alf[25 + 2]
will point to a possible unallocated address. It may not be The mistake, but may give headache– Jefferson Quesado
Another problem, I think this more serious:
alf
is not a null-terminated string. It is a 27-position vector, the first 26 being lowercase letters A to Z, and the position 26 is line breaking. In addition to this position, we are at invalid address for this object. Howalf
is not finished null, you will have problems callingstrlen(alf)
and saving the princess– Jefferson Quesado
As for the first bug, this I had already noted to fix, but this problem proved more serious; as for the second, I had not even noticed it, I will fix it immediately; thank you for helping with this.
– Miguel N.