LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 04-04-2007, 12:36 PM   #1
emge1
LQ Newbie
 
Registered: Sep 2006
Posts: 15

Rep: Reputation: 0
segfault caused depending on how a function is called


I have this function to trim whitespace which it segfaults
if i call it this way:
Code:
   /*bad*/
   btrimws("   a\n");
or this way:
Code:
   /*bad*/
   char *str1="   b\n";
   btrimws(str1);
but not this way:
Code:
   /*works ok*/
   char *str2= (char *)malloc(10);
   strcpy(str2,"   c\n");
   btrimws(str2);
or this way:
Code:
   /*works ok*/
   char str3[]= "   d\n";
   btrimws(str3);
what should i do? I think i know its segfaulting because I am trying to write over something that hasn't been allocated, but I am not 100% sure. Also I don't understand why or not quite sure what I can check for in my trim function to make it work for all cases.

thanks a lot in advance.




Code:
#define BLOCK_COPY(D,S,L)        { if ((L) > 0) memmove ((D),(S),(L)); }
#define WSPACE(c)                (isspace ((unsigned char) c))

int btrimws (char* b) {
   int i, j;
   if (b == NULL) return -1;
   int slen = strlen(b);
   if(slen <= 0) return -1;

   for (i = slen - 1; i >= 0; i--) {
      if (!WSPACE (b[i])) {
         if (b[i+1] != (unsigned char) '\0')
            b[i+1] = (unsigned char) '\0';      /* segfaults here*/
         for (j = 0; WSPACE(b[j]); j++) {}
         return bdelete (b, 0, j);
      }
   }
   b[0] = (unsigned char) '\0';
   slen = 0;
   return 0;
}
*note: this code is modified from the The Better String Library

Last edited by emge1; 04-04-2007 at 12:39 PM.
 
Old 04-04-2007, 12:37 PM   #2
emge1
LQ Newbie
 
Registered: Sep 2006
Posts: 15

Original Poster
Rep: Reputation: 0
here is the other function that gets called from the trim function.

Code:
/*  int bdelete (char* b, int pos, int len)
 *
 *  Removes characters from pos to pos+len-1 inclusive and shifts the tail of 
 *  the char* starting from pos+len to pos.  len must be positive for this 
 *  call to have any effect.  The section of the string described by (pos, 
 *  len) is clamped to boundaries of the char* b.
 */
int bdelete (char* b, int pos, int len) {
   /* Clamp to left side of bstring */
   if (pos < 0) {
      len += pos;
      pos = 0;
   }
   int slen = strlen(b);
   if (len < 0 || b == NULL || slen < 0 ) 
      return -1;
   if (len > 0 && pos < slen) {
      if (pos + len >= slen) {
         slen = pos;
      } else {
         BLOCK_COPY ((char *) (b + pos),
                     (char *) (b + pos + len), 
                     slen - (pos+len));
         slen -= len;
      }
      b[slen] = (unsigned char) '\0';
   }
   return 0;
}
 
Old 04-04-2007, 12:48 PM   #3
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
you cant modify a string literal.
 
Old 04-04-2007, 01:26 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by xhi
you cant modify a string literal.
So if you want it to work with literals then you need to change the signature of the function.Maybe a function that accepts a character array and returns the modified character array is what you want.

Code:
char * trim (const char *)

Last edited by graemef; 04-04-2007 at 01:28 PM.
 
Old 04-04-2007, 01:48 PM   #5
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by graemef
So if you want it to work with literals then you need to change the signature of the function.Maybe a function that accepts a character array and returns the modified character array is what you want.

Code:
char * trim (const char *)
i agree.. and another option would be a function that requires the return memory to be passed to the function.. allowing the return to notify of success..

ie
Code:
int trim(const char* input, char* output);
avoiding calls to malloc, this could be used as such
Code:
char buffer[STR_MAX];
if(!trim("    First char is F", buffer)
  printf("error trimming");
or something like that anyhow
 
Old 04-04-2007, 04:40 PM   #6
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie
Posts: 1,455

Rep: Reputation: 84
Hya,

In the old days, --fwritable-stings or something used to take care of this issue.

Happy Penguins!
 
Old 04-04-2007, 04:48 PM   #7
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by kaz2100
Hya,

In the old days, --fwritable-stings or something used to take care of this issue.

Happy Penguins!
that sounds like pure evil
 
  


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
sleeping function called from invalid context kushneeraj Programming 8 04-16-2013 09:25 PM
what is the kernel function called when a file is created viv_nan Linux - General 2 03-08-2007 07:09 AM
ANSI C trim function segfault every time rose_bud4201 Programming 4 02-27-2006 05:16 PM
howto know whether a function is called in signal handler snowing Programming 1 11-07-2005 10:55 PM
USB Driver Probe function not being called xtom Fedora 0 10-28-2005 04:48 PM


All times are GMT -5. The time now is 09:48 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration