The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
code segment trob UNIX for Dummies Questions & Answers 1 04-23-2009 08:33 PM
test command looks strange... jjinno Shell Programming and Scripting 2 11-02-2007 07:31 PM
extract segment mpang_ Shell Programming and Scripting 1 01-12-2007 01:33 AM
Strange error message with regex test... fgilain Shell Programming and Scripting 4 05-31-2006 09:52 PM
Segment Fault zhshqzyc High Level Programming 9 04-07-2006 11:47 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-24-2009
yanglei_fage yanglei_fage is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 25
a strange segment fault about ltp-posix test

Hi all

In the ltp-posix test,there is a case in open_posix_testsuite\conformance\interfaces\timer_gettime\speculative/6-1.c

Quote:
#include <time.h>
#include <stdio.h>
#include <errno.h>
//#include "posixtest.h"
#define BOGUSTID 9999
int main(int argc, char *argv[])
{
timer_t tid;
struct itimerspec its;
tid = (timer_t) BOGUSTID;

if (timer_gettime(tid, &its) == -1) {
if (EINVAL == errno) {
printf("fcn returned -1 and errno==EINVAL\n");
//return PTS_PASS;
} else {
printf("fcn returned -1 but errno!=EINVAL\n");
printf("Test FAILED\n");
//return PTS_FAIL;
}
}
printf("fcn did not return -1\n");
//return PTS_PASS;
}
I run the above code,it will has a segment fault, if I modify it to below,it works well
Quote:
#include <time.h>
#include <stdio.h>
#include <errno.h>
//#include "posixtest.h"
#define BOGUSTID 9999
int main(int argc, char *argv[])
{
timer_t tid;
struct itimerspec its;
int tval=BOGUSTID;
printf("%d\n",tval);
tid = (timer_t) &tval;
printf("%d\n",tid);
if (timer_gettime(tid, &its) == -1) {
if (EINVAL == errno) {
printf("fcn returned -1 and errno==EINVAL\n");
//return PTS_PASS;
} else {
printf("fcn returned -1 but errno!=EINVAL\n");
printf("Test FAILED\n");
//return PTS_FAIL;
}
}
printf("fcn did not return -1\n");
// return PTS_PASS;
}
Anybody can tell me why?
  #2 (permalink)  
Old 06-24-2009
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,859
Reducing the problem to its essence makes it easier to understand
Code:
#include <time.h>
#define BOGUSTID 9999

timer_t tid;
  
#if WANT_SEG_FAULT
   tid = (timer_t) BOGUSTID
#else
   int tval=BOGUSTID;
   tid = (timer_t) &tval;
#endif
The correct header is being included but an assumption is being made that tid is capable of storing a value as shown above. Generally this assumption is wrong as timer_t is specified to be an opaque type in POSIX.1. On most versions of GNU/Linux, timer_t is actually defined by
Code:
#define timer_t   void *
In your example you are trying to stuff 9999 into a void * - which is not on.

BTW, your workaround is defective also. tid is not being set to 9999 but to the address of tval.
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 05:42 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0