0

I am trying to write a function that returns a pointer copied from the initial function parameter but without withspaces in the beggining and in the end (' ', '\n', '\t').

char    *ft_strtrim(char const *s)
{

    unsigned char   *p;
    int             start;
    int             end;
    size_t          index;

    start = 0;
    end = ft_strlen(s) - 1;
    while (s[start] == '\n' || s[start] == '\t' || s[start] == ' ')
        start++;
    while (s[end] == '\n' || s[end] == '\t' || s[end] == ' ')
        end--;
    if (start == 0 && end == ft_strlen(s) - 1)
        return ((char*)s);
    if (end - start < 0)
        return (NULL);
    printf("%d\n", end - start);
    p = (char*)malloc(sizeof(char) * (end + 1 - start));
    index = 0;
    while(start <= end)
    {
        p[index] = s[start];
        index++;
        start++;
    }
    p[index] = '\0';
    return (p);
}

I want to return NULL when the value of end - start is below 0.Even though in the printf that I used after it to debug the function the value is negative,the function doesn't return NULL and when I compile the code I get segmentation fault.

7
  • 2
    If strlen(s) == 0, end == -1, and s[end] is undefined behavior.
    – user3185968
    Commented Oct 29, 2015 at 20:38
  • What is the definition of ft_strlen, and what input is being given to this function?
    – dbush
    Commented Oct 29, 2015 at 20:41
  • 1
    An all space string is also a problem. while (s[end] == '\n' || s[end] == '\t' || s[end] == ' ') end--; is UB. Commented Oct 29, 2015 at 20:44
  • "when I compile the code I get segmentation fault.' --> Do you mean when you run the code, you get a segmentation fault? Commented Oct 29, 2015 at 20:46
  • FYI (end - start < 0) can be simplified to (end < start). And the only case where this will happen is if the string is empty or all whitespace. And those are precisely the cases where your code causes undefined behavior, because the while loops don't stop when they reach the beginning or end of the string.
    – Barmar
    Commented Oct 29, 2015 at 20:48

3 Answers 3

0

I don't really know what to do when I find the answer to my problem,it's just my second question here.

I changed the condition in

if (end < start)
   return (NULL);

and used for end and start size_t type and know it works. Thank you very much for your comments.

1
  • The code before this answer's code while (s[end] == '\n' || s[end] == '\t' || s[end] == ' ') end--; is a significant problem. Commented Oct 29, 2015 at 21:14
0

Code is effectively doing a trim of leading and trailing whitespace, but has UB under a number of conditions, including all spaces.

 while (s[end] == '\n' || s[end] == '\t' || s[end] == ' ')
    end--;

The above code lead s to undefined behavior as end < 0 and s[end] is undefined behavior.

char *strtrim(char const *s) {
  // First first non-white-space
  while (isspace((unsigned char ) *s))
    s++;

  if (*s == 0) return NULL;

  // Find last non-WS
  const char *end = s;
  const char *last_non_space = s;
  while (*end) {
    if (!isspace((unsigned char ) *end)) {
      last_non_space = end;
    }
    end++;
  }

  // Allocate
  size_t length = (size_t) (last_non_space - s + 1);
  char *dest = malloc(length + 1);
  if (dest == NULL) return NULL;
  memcpy(dest, s, length);
  dest[length] = '\0';
  return dest;
}
0

Maybe instead of return (p);, you return p[index];? Just a suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.