KERN_INVALID_ADDRESS has me stumped - c

I keep getting this error trying to run the debugger:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
0x00007fff8c2414f0 in strlen ()
Here is my code:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char s2[25];
strcpy(s2, argv[1]);
int keyLen = strlen(s2);
printf("Please enter a string of text to be encrypted!\n");
string p = GetString();
for (int i = 0, n = strlen(p); i < n; i++)
{
if (isupper(p[i])){
int sum = (p[i] - 'A') + (s2[i % keyLen] - 'A');
char c = 'A' + sum%26;
printf("%c", c);
}
}
printf("\n");
printf("%d\n", keyLen);
}
I can compile the code with no errors and it works like it should. I am running the debugger to step into the for loop and look at what the math is doing to better understand it.

If GetString() returns null, then calling strlen(null) will give this error. Other errors on the part of GetString() could be causing this as well.

What is type 'string' in C?
Strlen() expects C-type array and not some custom 'string' type.
(+ there is a possibility of null input as pointed above)

I finally got it thanks to hmjd I was running it incorrectly I would start the program
gdb vignere HHHHH
Which is incorrect I ran it
gdb vignere
run HHHHHH
and it worked perfect!

Related

How do i add up all elements of a command line argument

here is my code
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char* argv[])
{
int a;
for(int i=1;i<=argc;i++){
a+=atoi(argv[i]);
}
printf ("%d",a);
}
I keep getting segmentation faults but i am trying to add up all elements of the command line so for example ./a.out 5 6 7 would give 18 as the output, cheers.
The problem (with the crash) is the loop itself:
for(int i=1;i<=argc;i++)
The argc argument is the number of arguments passed to the program, including the "program name" at argv[0]. So valid indexes for the actual arguments are argv[1] to argv[argc - 1].
Furthermore the argv array is terminated by a null pointer, which will be at argv[argc].
Since you include argv[argc] in your loop you pass a null pointer to atoi which leads to undefined behavior and likely crashes.
The simple solution is to use less-than < instead of less-than-or-equal as the loop condition:
for(int i=1;i<argc;i++)
You never initialized a to 0. Also, use strtol() function.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int a = 0;
for (int i = 1; i < argc; i++) {
a += strtol(argv[i], NULL, 10);
}
printf("%d\n", a);
return EXIT_SUCCESS;
}

Segmentation Fault (core dump) with Files (C- linux)

I'm getting a segmentation error (core dump) when I try to run this. It compiles perfectly but I get the error, and I don't know why. There must be a problem with a file writing because without this works good. Any help would be great. Thanks!
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <crypt.h>
#include <string.h>
int
main(void)
{
FILE *f=fopen("shadow1.txt","w");
if (f=NULL)
{
printf("ERROR");
}
unsigned long seed[2];
char salt[] = "$1$........";
const char *const seedchars =
"./0123456789ABCDEFGHIJKLMNOPQRST"
"UVWXYZabcdefghijklmnopqrstuvwxyz";
char *password;
int i;
/* Generate a (not very) random seed.
You should do it better than this... */
seed[0] = time(NULL);
seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000);
/* Turn it into printable characters from ‘seedchars’. */
for (i = 0; i < 8; i++)
salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f];
/* Read in the user’s password and encrypt it. */
password = crypt(getpass("Password:"), salt);
/* Print the results. */
//fprintf(f,"%s $ %s",password);
printf("Success Registration to file !");
fclose(f);
return 0;
}
if (f=NULL)
{
printf("ERROR");
}
was the problem...
void Register(char u,char p) {
you probably want these to be char * because of the fprintf that treats them as strings:
fprintf(f,"%s $ %s",u,p);
and since you pass char *s in:
char *password,*username;
//...
Register(username,password);
This would most likely have been caught by compiler warnings. It is a lot faster to get your answer from the compiler than from here.
If you can't figure out why your program isn't working, you can enable all the warnings you should need with -Wall -Wextra and turn warnings into errors with -Werror.
You are not allocating space to hold username so it will segfault on the scanf.

Segmentation fault (core dumped) with the strcpy-function in c

I am trying to write a function which will read two strings stringArray[MAX]="ABADDFDEFBFCCHCGGEHJJI" and popArr[MAX]="ABCDEFGHIJ" and generate an output like this:
A
B-F-D-A
C-F-D-A
D-A
E-G-C-F-D-A
F-D-A
G-C-F-D-A
H-C-F-D-A
I-J-H-C-F-D-A
J-H-C-F-D-A
However I'm getting a Segmentation fault (core dumped) Error. Why? This is my code:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100
size_t strlstchar(const char *str, const char ch)
{
char *chptr = strrchr(str, ch);
return chptr - str;
}
int main(){
// Input strings
char stringArray[MAX]="ABADDFDEFBFCCHCGGEHJJI";
char popArr[MAX]="ABCDEFGHIJ";
int index=2, lenpop, lentemp;
char usedString[MAX]="";
char tempChar;
lenpop = strlen(popArr);
printf("%c\n", stringArray[0]);
for(int i=1;i<lenpop;i++){
strcpy(usedString, stringArray);
printf("%c", popArr[i]);
tempChar = popArr[i];
while(tempChar!=stringArray[0]){
while(index%2==0){
index = strlstchar(usedString, tempChar);
lentemp = strlen(usedString);
usedString[lentemp-index-1]=0;
}
printf("-%c", usedString[index-1]);
tempChar=usedString[index-1];
index=2;
}
printf("\n");
}
return 0;
}
Thanks in advance!
The segmentation violation occurs in this line:
usedString[lentemp - index - 1] = 0;
Here, you're trying to find the index from the end, but your strlstchar returns the index from the beginning, although it starts searching from the end. And you want truncate the string at the found character, of course.
Replace this line with just:
usedString[index] = 0;
and you get the desired output.

Segmentation fault (core dumped) error for C program

I am trying to run below program in an online C compiler. But I get segmentation error. Can you help me fix this
#include <stdio.h>
#include <string.h>
int main()
{
char string[15] = "Strlwr in C";
printf("%s",tolower(string));
return 0;
}
Following is the prototype of tolower
int tolower(int c);
You should pass an int or something like char which can safely convert to int. Passing char * (Type of string) like you do leads to UB.
To convert a string to lowercase, you need to convert each character separately. One way to do this is:
char string[15] = "Strlwr in C";
char lstr[15];
int i = 0;
do {
lstr[i] = tolower(string[i]);
} while(lstr[i] != '\0');
printf("%s", lstr);
You are using tolower incorrectly. This function returns int and gets int as a parameter (here is it's declaration: int tolower(int c);). What you want to do is call it on each char of your char array, and print each one:
char string[15] = "Strlwr in C";
for(int i = 0; i < strlen(string); i++)
printf("%c",tolower(string[i]));
Read cplusplus.com/reference/cctype/tolower It takes a single int as parameter, not char and not array.
You probably want to use a loop on "string", which processes each in turn.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
int i;
char string[15] = "Strlwr in C";
for (i=0; i< sizeof(string)/sizeof(char); i++)
{
string[i]=(char)(tolower((int)string[i]));
}
printf("%s\n",string);
return 0;
}
Output:
strlwr in c

Why isn't isalpha working?

I'm working with C and I need to check that the user inputed second command line argument argv[1] is made up of only alphabetical charchaters and if not, to do what is inside the else loop. I used the is alpha function but when i compile and run the program no matter what my second command line argument is (alphabetical or otherwise), its always executing the "else loop". How do i fix this?
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
int a = argc;
if (a != 2)
{
return 1;
}
string b = argv [1];
int c = strlen(b);
string m;
for (int i = 0; i < c; i++)
{
if (isalpha(b[c]))
{
m = GetString();
}
else
{
printf("Please provide a valid keyword\n");
return 1;
}
}
}
Try replacing
if (isalpha(b[c]))
with
if (isalpha(b[i]))
Currently you are checking the element at the index which is the result of strlen(b) at every iteration of your loop. Because array indices are zero based in C b[strlen(b)] is referencing '\0', the null terminator.
In reference to the Keith Thompson comment below and the answer to this question you should actually be casting the value passed to isalpha to an unsigned char to ensure that undefined behaviour is not invoked.
Thus you should change your code to
if (isalpha((unsigned char)b[i]))
to ensure there is no UB
Use isalpha(b[i]) instead of isalpha(b[c])
like this:
if (isalpha(b[i]))
{
m = GetString();
}

Resources