This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH v4][BZ #15346] Allow leading and trailing spaces in getdate


> +  size_t inlen, oldlen;
> +  bool free_instr = false;

The general preference is not to put a local's definition any earlier than
it needs to be, i.e. put inlen/oldlen where they are initialized below and
put free_instr immediately above the if block that might set it.

> +  char *instr;
> +
> +  if (inlen == oldlen)
> +    instr = (char *) string;

It's nice to avoid de-consting casts.  e.g. replace INSTR with
COPIED_STRING, set to NULL in the default case.  Then do:

  char *copied_string = NULL;
  if (inlen < oldlen)
    {
      bool using_malloc = false;
      if (__libc_use_alloca (inlen + 1))
	copied_string = alloca (inlen + 1);
      else
	{
	  copied_string = malloc (inlen + 1);
	  if (copied_string == NULL)
	    {
	      fclose (fp);
	      return 6;
	    }
	  using_malloc = true;
	}
      memcpy (copied_string, string, inlen);
      copied_string[inlen] = '\0';
      string = copied_string;
      if (!using_malloc)
	copied_string = NULL;
    }

and:

  if (copied_string != NULL)
    free (copied_string);

I'd prefer these cleanups, but the change is now OK either way.


Thanks,
Roland


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]