UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2007-08-16 09:39 PM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: Seg Fault

regex_t rgx;
    regmatch_t matches[8];

	char pattern[] =   "<wsse:BinarySecurityToken Id=\"Compact1\">(.*)</wsse:BinarySecurityToken>(.*)<wst:BinarySecret>(.*)</wst:BinarySecret>(.*)<wsse:BinarySecurityToken Id=\"PPToken2\">(.*)</wsse:BinarySecurityToken>(.*)<wsse:BinarySecurityToken Id=\"Compact3\">(.*)</wsse:BinarySecurityToken>(.*)<wsse:BinarySecurityToken Id=\"Compact4\">(.*)</wsse:BinarySecurityToken>(.*)<wsse:BinarySecurityToken Id=\"Compact5\">(.*)</wsse:BinarySecurityToken>(.*)<wsse:BinarySecurityToken Id=\"Compact6\">(.*)</wsse:BinarySecurityToken>(.*)";
    if (regcomp (&rgx, pattern, REG_EXTENDED) != 0)
		{
			printf("Errortje");
			exit(1);
		}
	
	  regexec (&rgx, dtest, 8, matches,0) ? printf ("No match\n") : printf ("match\n");
    //if (regexec (&rgx, dtest, 8, matches, 0)) {
        // Wattefap er nu aan de hand is I don't weten, But it hasn't gematched doen ..
    //    printf("Test,, zoals je ziet als je dit prog runt, de regex matched niet ..\r\n");
    //}
	
	size_t rm, i, ichar, curChar;

	char *matchesArr[8];
	
	//printf("%s\r\n----------------------------------------\r\n", dtest);
	
	for (i = 1; !rm && i <= rgx.re_nsub; i++)
    {
		curChar = 0;
   		for(ichar = matches[i].rm_so; ichar < matches[i].rm_eo; ichar++){
		//printf("%d:The regex matches from %d to %d.\n",i,
          //  matches[i].rm_so, matches[i].rm_eo);
			strcat(&matchesArr[i][curChar], &dtest[ichar]);
			curChar++;
		}
		//strcat((char *)&matchesArr[i][curChar], '\0');
			
    }
	
    regfree (&rgx);

Offline

#2 2007-08-16 10:52 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Seg Fault

Sigh...

Please, compile with compiler warnings enabled, and fix those before trying anything else.

For instance, 'rm' is used uninitialized, surely the compiler complains about that.
Starting 'i' at 1 instead of 0 seems like an error too.

And please, if you post code, please something complete so we can compile it and run it if we want to test it ourselves.

And Valgrind is a great tool too, use it. But for segfaults just use gdb. Run your program through gdb and when it crashes gdb will tell you exactly where it went wrong. Just compile with debugging info enabled (-g).

Offline

#3 2007-08-17 08:15 AM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: Seg Fault

Okay, but, I did compile it with the -Wall option, and I got a few errors more, but these where not in that piece of code. Those errors are being fixed, but not relevant for this piece of code. And I thought it wasn't showing any kind of other errors.

I can't post full code here, because, this is for a MSN messenger, and this is just the 'testing-phase' so it isn't dynamical yet. The usernames and passwords are stored in the C file, and I don't want them to be right on the internet. (Microsoft itself wouldn't be as safe as it should be, but.. ;) )

Offline

#4 2007-08-17 10:25 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Seg Fault

Then you edit/remove the username and password before posting everything? I don't see the problem...

Offline

#5 2007-08-17 10:33 AM

niek
Member
Registered: 2006-12-24
Posts: 109

Re: Seg Fault

Offline

#6 2007-08-17 01:17 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Seg Fault

There are so many things wrong with such a short piece of code, that I'm at a loss
as to where to begin! ;-)  First of all, I take it you're not an experienced C programmer?
I'm not trying to be insulting, but looking at what you've got written there (along with
past code from prior threads) makes it pretty clear that you don't understand some
simple basic C concepts, such as pointers and their need to have memory manually
allocated for them...  C can be a very dangerous and unforgiving language for those
who don't know it well, so it's generally a bad idea for anyone unskilled to use it for
writing network apps of any sort, since they'll be almost guaranteed to be buggy and
remotely exploitable...  It's often tough even for those of us who are intimately familiar
with it and have been doing it for many, many years...

Anyway...  I'll give a try at detailing all the obvious problems I spot:

i3839 already covered two biggies: uninitialized "rm" and starting "i" at 1 instead
of the proper 0...

for (i = 1; !rm && i <= rgx.re_nsub; i++)
    {
		curChar = 0;
   		for(ichar = matches[i].rm_so; ichar < matches[i].rm_eo; ichar++){
			strcat(&matchesArr[i][curChar], &dtest[ichar]);
			curChar++;
		}
    }

You never allocate any memory for "matchesArr* ", yet you go on treating each
element like a valid string you can strcat() to...

Not to mention, your strcat() is completely and utterly WRONG in every conceivable
way...  You don't seem to understand how strcat() works...  It doesn't just tack on
a single character at a time; it tacks on an entire null-terminated string...

Look, it'll just be less work and less headache for me to simply demonstrate a
proper way of doing what you want here, than to try to explain all the problems
with what you've done...  So, here you go, a full replacement of the above block:

for (i = 0; (i < 8) && (matches[i].rm_so >= 0); i++) {
    int len = matches[i].rm_eo - matches[i].rm_so;
    matchesArr[i] = malloc (len + 1);
    memcpy (matchesArr[i], dtest + matches[i].rm_so, len);
    matchesArr[i][len] = '\0';
}

After that, "matchesArr" will contain your array of substrings...  You will want to
free() each allocated element of the array when finished with them...

Offline

Board footer

Powered by FluxBB