If I had this code all inside just one function, I could get everything working fine, but now my problem is that I can't read the results of my regex. When I try printing the first result, I get a segmentation fault. Instead, I was expecting the first regex result to appear of "T/2/b".
Also, the word "Matches" appears on the screen right before the segmentation fault occurred.
Is there something I'm missing?
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
typedef struct{
char str[1000];
} regex;
long regexmatch(const char* str,const char* regexs,size_t nummatch,regex** reg){
regex_t r;regmatch_t match[nummatch];
if (regcomp(&r,regexs,REG_EXTENDED) != 0){return -1;}
if (regexec(&r,str,nummatch,match,0)!=0){regfree(&r);return -1;}
size_t i=0;
for (i=0;i<nummatch;i++){
if (match[i].rm_so > -1){
memset((**reg).str,0,1000);
memcpy((**reg).str,(char*)(str+match[i].rm_so),match[i].rm_eo-match[i].rm_so);
(*reg)++; //this is where I load the result into the struct and advance the pointer.
}
}
char *p=(**reg).str;
*p='\0';
regfree(&r);
return 0;
}
int main(){
char buf[1000];
memset(buf,0,1000);
regex* r=(regex*)buf;
if (regexmatch("T/2/b","([A-Z]+)/([0-9]+)/([a-z]+)",10,&r)==0){
printf("Matches\n");
printf("%s\n",r->str); //causes segmentation fault. Expecting a "T/2/b" to be displayed instead.
}
}
Related
So I'm getting to know valgrind a bit and I've come across an example that has me confused.
I was shown this code causes a Invalid Write in valgrind:
#include <stdlib.h>
#include <stdlib.h>
int main(int argc, const char* argv[])
{
int index = atoi(argv[1]);
char *x = malloc(10);
x[index] = âfâ;
return 0;
}
while this code causes a segfault in valgrind:
int foo(y)
{
char str[10];
str[11] = âfâ;
return 0
}
So do these examples have anything to do with the fact that example 1 is something thats done on the heap while the 2nd example is done on the stack?
Am I missing something bigger?
Thanks
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.
Case 1:
When I take string input, it successfully gives the output, writing this piece of code:
#include <stdio.h>
int main()
{
char *str;
scanf("%s",&str);
printf("%s",&str);
return 0;
}
Case 2:
On the other hand, it throws a Runtime Error for this snippet:
#include <stdio.h>
int main()
{
char *str;
scanf("%s",&str);
printf("%s",str);
return 0;
}
I found this thing peculiar, and want to know why it happens...
Thanks in advance.
None of those two cases are right.
Case 1 only worked because you got lucky, probably by giving a short string as input. Try something like "bfjabfabjkbfjkasjkvasjkvjksbkjafbskjbfakbsjfbjasbfjasbfkjabsjfkbaksbfjasbfkja" and you'll suffer a seg fault, most likely.
You should have a block of memory associated with str, either on the stack by declaring an array for it or on the heap malloc'ing memory for it.
And you shouldn't use the & operator.
So it would go like this:
#include <stdio.h>
int main()
{
char str[50]; // 50 is arbitrary
scanf("%s",str);
printf("%s",str);
return 0;
}
or like this:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char* str = malloc(50); // 50 is arbitrary
scanf("%s",str);
printf("%s",str);
free(str);
return 0;
}
I'm trying to make a C program work and I'm getting mad. This is my code simplified to find the error:
#include <stdio.h>
#include <unistd.h>
#include <sqlite3.h>
int main(){
sqlite3 *conn;
sqlite3_stmt *res;
const char *tail, *sqlresult;
sqlite3_open("cubecat", &conn);
char buffer,query;
int id;
id= 1;
buffer = 'a';
if(buffer == 'a') snprintf(&query,100,"SELECT start FROM payloads WHERE id=%d", id);
printf("%s",&query);
int error = sqlite3_prepare_v2(conn, &query, 100, &res, &tail);
printf("%d",error);
}
The error is exactly on "sqlite_prepare_v2" function, because if I comment that line, there's no Segmentation Fault.
Thank you in advance!
char query;
snprintf(&query,100,"SELECT start FROM payloads WHERE id=%d", id);
This is what's wrong. query only reserves memory for one character. There's a reason the 2nd argument of snprintf() specifies the size. This code should be modified like this:
char query[100];
snprintf(query, sizeof(query), "SELECT start FROM payloads WHERE id=%d", id);
I was trying to mimic strtok functionality but getting segmentation fault. Please help me out here.
Here is my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char argv[])
{
int i=0;
char c[]="get the hell out of here";
char *p;
char *temp=(char *)malloc(100);
while(c[i]!='\0')
{
if(c[i]!=' ')
{
*temp=c[i];
temp++;
i++;
}
else
{
*temp='\0';
printf("printing tokenn");
puts(temp);
i++;
temp="";
}
}
return 0;
}
temp="";
This causes temp to point at unmodifiable memory, leading to a fault the next time you try to modify through it. You wanted to restore temp to the value you got from malloc (which you forgot to save).