LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-04-2013, 09:47 AM   #1
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Rep: Reputation: Disabled
regex.h - regcomp, regfree


I am creating regular expressions thoughout my program and have noticed that if I try to free a regex_t without having first set it with regcomp, it causes the program to segfault. Is there any way to test to see if it has been set to eliminate any possibility of bringing my program down? Checking the address for null doesn't seem to work, and try-catch doesn't stop the abend.

Thanks.

Brandon
 
Old 03-05-2013, 02:10 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,510

Rep: Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456Reputation: 3456
would be nice to see your code. also you can try valgrind to catch memory related problems.
 
Old 03-05-2013, 08:09 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,609

Rep: Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105
Perhaps you should initialize your pointers with NULL:

Code:
regex_t *preg= NULL;
...
if (preg) { regfree (preg); preg= NULL; }
 
Old 03-05-2013, 08:46 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170
Always make sure that all pointers are somehow initialized to "known NULL." In all cases, they must either point to a valid object or be NULL.

Immediately after freeing anything ... in the very next statement ... set its pointer back to NULL.

(Omitting the bit about "the very next statement" once cost me $10,000 USD in real money. And I wasn't even the one who had done it. Long story.)
 
Old 03-05-2013, 10:57 AM   #5
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Original Poster
Rep: Reputation: Disabled
I think you guys have exactly pinpointed what I have to do.

Also, I cannot let you tease us with what sounds like such an interesting story. How did a programming error cost you money? Did it cost you a client or a sale?
 
Old 03-06-2013, 09:46 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170
The programming work that my company (http://www.sundialservices.com) still does, is covered under warranty. We promise the customer that the software will work. This particular error happened in the Windows 3.1 days and it was caused, unbeknownst to us, by a double-free in a Delphi program, code that looked like this:
Code:
 
  a.free;
  b.free;
  a := nil;
  b :=nil;
Do you see the error? The destructor of "b" also included a.free; and the pointer was still non-nil therefore a double-free occurred. To resolve the problem, the second and third statements above were transposed.

It was not in code that we had written; nevertheless, a promise is a promise. The result of the error was that, one moment our program was running happily; next moment, you were looking at a DOS prompt and Microsoft Windows was completely gone.

I couldn't resolve the problem timely. I had to give the money back. I couldn't in good faith keep it.

It made a positive impression, though, because there aren't too many programming shops who will do that. The client actually remained a client for many years to come, and they didn't use Windows 3.1 much longer after that. But, "an elephant's faithful one hundred percent."

Last edited by sundialsvcs; 03-06-2013 at 09:49 AM.
 
Old 03-06-2013, 09:49 AM   #7
Brandon9000
Member
 
Registered: Apr 2012
Location: Florida
Distribution: Many
Posts: 111

Original Poster
Rep: Reputation: Disabled
Thanks. Sometimes showing character is worth losing some initial money. I can think of at least one person I know who became very rich by having a "customer is always right" policy. Thanks again for your response to my question.
 
Old 03-06-2013, 09:59 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170Reputation: 3170
Uh huh. Been doin' this for 21 years now; ain't changing business practices now.

(P.S. bit of a lull 'round here now ... the crickets are a'callin'. PM me if you can change that.)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 02:10 AM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 09:08 PM
use of regcomp and regexec sancho1980 Programming 5 03-02-2009 11:55 AM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 06:10 AM
[SOLVED] Problems using regex 'es: regcomp/regexec dedec0 Programming 5 06-15-2007 11:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration