segfault caused depending on how a function is called
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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;
}
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.
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");
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.