I have a little C program.
#include <stdlib.h>
#include <stdio.h>
void counter(int n);
int main(int argc, char *argv[])
{
int n = atoi(argv[1]);
counter(n);
return 0;
}
void counter(int n)
{
printf("%d\n", n);
if(n == 0) {
return;
}
counter(n - 1);
}
When I enter 200000 as the command line argument it works well. But with 300000 it says: 17623 segmentation fault (core dumped).
With gdb, the error is: Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a6f198 in _IO_new_file_write (f=0x7ffff7dd0760 <_IO_2_1_stdout_>, data=0x555555756260, n=6) at fileops.c:1196 1196 fileops.c: No such file or directory.
I don't know what is wrong. Should I allocate memory or something like that?
Related
I am using the GMP. My program can build successfully, But run failed. The following is error things:
a=1231231231231231
res^n != a
Segment fault
All codes in my program is:
#include <gmp.h>
#include <stdio.h>
int main()
{
mpz_t a,res;
unsigned long int n = 123;
char str1[] = "1231231231231231";
mpz_init_set_str(a, str1, 10);
gmp_printf("a=%Zd\n",a);
mpz_init(res);
if(mpz_root(res, a, n)){
printf("res^n == a\n");
}
else{
printf("res^n != a\n");
}
mpz_clears(a,res);
return 0;
}
You have to call mpz_clears() like:
mpz_clears(a,res, NULL);
Here's what the documentation says:
Function: void mpz_clears (mpz_t x, ...)
Free the space occupied by a NULL-terminated list of mpz_t variables.
I am trying to work with multi-threaded programs, and I am getting an error with the pthread_join function. The output from this code is:
after pthread_create
Segmentation fault (core dumped)
And here is the code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *myfunc1()
{
// code segments
pthread_exit(sum1);
}
void *myfunc2()
{
// code segments
pthread_exit(sum2);
}
int main(int argc, char *argv[])
{
void *sum1, *sum2;
pthread_t thread_id1,thread_id2;
pthread_create(&thread_id1,NULL,myfunc1,NULL);
pthread_create(&thread_id2,NULL,myfunc2,NULL);
printf("after pthread_create\n");
pthread_join(thread_id1, &sum2);
pthread_join(thread_id2, &sum1);
printf("after pthread_join\n");
float firstSum = *((float *)sum1);
float secondSum = *((float *)sum2);
printf("Sum is %.5f\n\n", firstSum+secondSum);
return 0;
}
sum1 and sum2 are not initialised. So these lines
float firstSum = *((float *)sum1);
float secondSum = *((float *)sum2);
dereference undefined pointers.
Your thread functions should pass a pointer back (to something that survives the exit of the function) which can then be used by pthread_join e.g.
void *myfunc1()
{
float *ret = malloc(sizof *ret);
*ret = 3.14;
// code segments
pthread_exit(ret);
}
Then in main
float *sum1;
// run the thread
pthread_join(thread_id2, (void**)&sum1);
float result = *sum1;
free(sum1);
Your segmentation fault happens in one of your threads. pthread_create() creates and launches your thread, pthread_join() makes your main thread wait for the end of the other threads. Your main thread keeps running and starts waiting for your other threads to end, but one of them creates a segmentation fault so your main thread does not display "after pthread_join". So the segmentation fault does not come from pthread_join().
I am constantly getting "Segmentation fault (Core dumped)" error in the following c program. Please help me debug the program.
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
int numcmp(const char *a,const char *b){
if(atoi(a)==atoi(b)) return 0;
return -1;
}
int isequal(char *a,char *b,int (*cmp)(const char *,const char *)){
if(!cmp(a,b)) return 1;
return 0;
}
int main(int argc,char *argv[]){
if(argc!=3){
puts("Usage: Compare two string alphabeticaly or numericaly");
printf("Syntax: %s string1 string2\n",argv[0]);
exit(-1);
}
if(isequal(argv[1],argv[2],isalpha(argv[1])?strcmp:numcmp))
printf("%s and %s are equal\n",argv[1],argv[2]);
else
printf("%s and %s are not equal\n",argv[1],argv[2]);
}
isalpha only works for chars, not whole strings. You could write a function for that:
/* 1 - only letters, 0 - other characters */
int str_isalpha(const char* str) {
while (*str)
if (!isalpha(*(str++)))
return 0;
return 1;
}
and then use it appropriately. But actually, I don't see a reason for using numcmp here at all. Comparing ints and const char*s should yield the similar results.
I am getting a segmentation fault on the execution of the code below.The program compiles successfully ,but gives an error on execution.
What is a segmentation fault and how to correct it?
#include<stdio.h>
#include<malloc.h>
#include "sel.h"
#include<sys/time.h>
main(int argc,char *argv[])
{
struct timeval t1,t2;
struct timezone tz;
int i,n;
int *a;
char *num;
FILE *fp;
unsigned long long time1,time2;
fp=fopen("file1.txt","w");
sscanf(argv[1],"%d",&n);
a=(int*)malloc (sizeof(int)*n);
for(i=n;i>0;i--)
{
sprintf(num,"%d\n",i);
fputs(num,fp);
}
fclose(fp);
fp=fopen("file1.txt","r");
i=0;
while(fgets(num,255,fp)!=NULL)
{
sscanf(num,"%d",&a[i++]);
}
gettimeofday(&t1,&tz);
time1=t1.tv_sec*1000000+t1.tv_usec;
sel(a,n);
gettimeofday(&t2,&tz);
time2=t2.tv_sec*1000000+t2.tv_usec;
fclose(fp);
fp=fopen("file2","w");
for(i=0;i<n;i++)
{
sprintf(num,"%d\n",a[i]);
fputs(num,fp);
}
free(a);
printf("\n %llu",time2-time1);
}
Segmentation fault means you had a bad memory access. In this case, you never allocated any memory for num, so you are writing to an uninitialized pointer.
I am trying to run this piece of vulnerable C code:
#include <stdio.h>
#include <stdlib.h>
int add(int x, int y)
{
int z =10;
z = x + y;
return z;
}
main(int argc, char **argv)
{
int a = atoi(argv[1]);
int b = atoi(argv[2]);
int c;
char buffer[100];
gets(buffer);
puts(buffer);
c = add(a,b);
printf("Sum of %d+%d = %d\n",a, b, c);
exit(0);
}
I am trying to get past the segmentation fault so that I can input the integers but the segmentation fault prevents that. In the terminal I have tried:
gcc -ggdb -fno-stack-protector -U_FORTIFY_SOURCE -Wa,--execstack -o SimpleDemo SimpleDemo.c
I still get a segmentation fault. I am lost as to what to try next. As you can probably tell, I am an ubuntu newb. The bash code I am using comes from here:
http://www.evanjones.ca/buffer-overflow-101.html
I have been at this for while so would really appreciate some help
Cheers
Just explaining what hmjd is asking you to do.
Run (binary) 10 20