Re: trim function dumping core



raxitsheth2000@xxxxxxxxxxx wrote:
Joe/rkk,

check following main with your ltrim/rtrim/trim/trim_all...

int main()
{
char *p=NULL;
ltrim(p);

rtrim(p);

return 0;
}

I am not sure what should be the behaviour of l/r/all trim against NULL
ptr, on my sys is Seg Fault.(I am knowing why)

Should we check ?

char* (l/r) trim (char *s)
{
if (s==NULL) return NULL;
your remaining code of ltrim/rtrim;
}

this will ensure that your trim function is stable against one more
type of Bad-Input, (like one more testcase)
Even it is more likely that program may crash, but not in
ltrim/rtrim/all_trim written by you.

any thought on this ?


--raxit sheth


String functions should behave gracefully when given strings to deal with. Even zero length strings. It is up to the caller to ensure against NULL pointers passed to string functions, IMO.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
.



Relevant Pages

  • Re: trim function dumping core
    ... I am not sure what should be the behaviour of l/r/all trim against NULL ... ptr, on my sys is Seg Fault. ... char* trim ...
    (comp.lang.c)
  • Re: trim whitespace v3
    ... allocation. ... Trim removes bytes and never adds them, so you need to allocate one ... char *trim ... if (!dst) return NULL; ...
    (comp.lang.c)
  • Re: trim whitespace v3
    ... you want to trim spaces off the start until you hit a non-space, ... char *trim(const char *src) ... char *dst; ...
    (comp.lang.c)
  • Re: trim whitespace v3
    ... single pass in-place trim function ... void trim(char *src) { ... char* cur = src; ... int main { ...
    (comp.lang.c)
  • Re: String copy with pointers not working as expected
    ... be able to get working without the use of string functions such as ... int main ... char *p; ... the 'q' pointer... ...
    (comp.lang.c)