Does someone know why the following lines of code throws a *** stack smashing detected *** error
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
char x[16];
strcpy(x,"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
}
but the following code does not throw it?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
char x[16];
x[17] = 'a';
}
Thank you!!
Overwriting x[17] doesn't overwite the canary-value put before the return address by gcc.
Related
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include <unistd.h>
#include <ctype.h>
#include <assert.h>
void *process(char **nbE)
{
char buffer[8] = "test";
*nbE = &buffer[0];
printf("%s\n", *nbE);
}
int main(int argc, char **argv)
{
char *str;
process(&str);
printf("%s\n", str);
}
I'm trying to get the value of *nbE in main() by making it points to the address of first char in my array.
But it returns something not encoded, why?
What would be a way for me to do this way?
Note: I know I can do it simpler, I have a more complex code and this is a mini example
Basically I have something interesting in my array and want to pass it to my main function through a char* variable
char buffer[8] = "test";
creates a string that is local to the function, it is destroyed once you return from that function. Do this
static char buffer[8] = "test";
or
char * buffer = strdup("test");
you have to release the string when you have finsihed with it in the second case
I'm trying to write a program that takes a string as a command line argument and then runs said argument through a function (str_to_int) that takes a string as an input. However, when I try to compile the program, I get a warning saying
initializing 'char *' with an expression of type 'int' [-Wint
conversion]
char* str = atoi(argv[1]);
^ ~~~~~~~~~~~~~
And when I run the program I get a segmentation fault
I've tested the str_to_int a lot so I'm pretty sure that the issue lies with the command line program. Here's the code for it.
#include "hw3.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
char* str = atoi(argv[1]);
printf("%d\n", str_to_int(str));
return 0;
}
Can anyone tell me what I'm doing wrong? Thanks.
This is all you need, though it will crash if you leave out the command-line argument.
{
printf("%d\n", str_to_int(argv[1]));
return 0;
}
This is more robust:
int main(int argc, char *argv[])
{
if (argc == 1)
printf("missing parameter.");
else
printf("%d\n", str_to_int(argv[1]));
return 0;
}
#include "hw3.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
char* str = argv[1];
printf("%d\n", str_to_int(str));
return 0;
}
just remove atoi function invocation and it should work
i want write program in C via bluez API
I have used this site for tutorial :
and this is my code :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
int main(int argc, char **argv)
{
int dev_id = hci_get_route(NULL);
int res_scan=NULL;
int count;
inquiry_info *device_info=NULL;
res_scan = hci_inquiry(dev_id,3,255,NULL,&device_info,IREQ_CACHE_FLUSH);
printf("%i\n",res_scan);
for(count = 0;count < res_scan;count++)
{
char *name;
printf("count Before : %i\n",count);
ba2str(&(device_info+count)->bdaddr,&name);
printf("count After : %i\n",count);
printf("%s\n",&name);
}
}
and out console :
2
count Before : 0
count After : 1111833143
00:17:EB:5D:1B:86
why count value after ba2str(&(device_info+count)->bdaddr,&name); get random value ?
in that source i linked this issue wont occur !?
instead of
char *name;
...
printf("%s\n",&name);
use
char name[248] = { 0 };
...
printf("%s\n",name);
You need allocate memory before pass the variable as reference, and the best option is to do that out of the loop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
int main(int argc, char **argv)
{
int dev_id = hci_get_route(NULL);
int res_scan=NULL;
int count;
char *name = (char *) malloc(248*sizeof(char));
inquiry_info *device_info=NULL;
res_scan = hci_inquiry(dev_id,3,255,NULL,&device_info,IREQ_CACHE_FLUSH);
printf("%i\n",res_scan);
for(count = 0;count < res_scan;count++)
{
printf("count Before : %i\n",count);
ba2str(&(device_info+count)->bdaddr,name);
printf("count After : %i\n",count);
printf("%s\n",name);
}
free(name);
}
doing that your code will be faster because you will allocate memory only one time.
How can I call notify-send from C code with a message stored in my string ?
#include <stdlib.h>
int main(int argc, char *argv[])
{
system("mount something somewhere");
system("notify-send message");
return 0;
}
Just send the string as a parameter to system().
For example:
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char command[100], msg[100];
strcpy(command,"notify-send ");
strcpy(msg,"\"Hello World\"");
strcat(command,msg);
system(command);
return 0;
}
#include <stdio.h>
int main(void)
{
system("notify-send Test \"Hello World\"");
return 0;
}
I'm trying to compile three files together, having one main method in passweb.c.
heres passweb.c
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <cipher.c>
#include <menu.c>
long pointer;
char *createRecord(char *name, char *password, char *type);
char *file = "password.csv";
int main(int argc, char *argv[]){
if(fopen(file,"r")==NULL){
FILE *newFile = fopen(file,"w+");
fclose(newFile);
}
if(strcmp(argv[0],"-menu")==1){
menu();
}
else if(strcmp(argv[0],"-add")==1){
add(argv[1], argv[2], argv[3]);
}
else if(strcmp(argv[0],"-edit")==1){
edit(argv[1],argv[2],argv[3],argv[4],argv[5],argv[6]);
}
}
and cipher.c
#include <stdio.h>
#include <stdlib.h>
int Encrypt(char *fileName){
int offset=5;
Shift(fileName, offset);
}
int Decrypt(char *fileName){
int offset=-5;
Shift(fileName, offset);
}
the makefile:
passweb: passweb.c menu.c cipher.c
gcc -o passweb passweb.c menu.c cipher.c -I.
the errors:
passweb.c:10: error: conflicting types for ‘main’
./cipher.c:3: error: previous definition of ‘main’ was here
I can't figure out what I'm doing wrong.
Thank you in advance for your time!!
Don't include source files into source files. Get rid of the following:
#include <cipher.c>
#include <menu.c>
The way you've written it, you're compiling menu.c and cipher.c twice. First when you compile passweb.c, and again when you compile menu.c and cipher.c.