<<< Date Index >>>     <<< Thread Index >>>

Re: sscanf (x, "%u", &(uint64_t))



Hi,
On Sat, Mar 19, 2005 at 04:21:45PM +0900, TAKAHASHI Tamotsu wrote:
> Is it Okay to sscanf (x, "%u", &(uint64_t)) ?
> I got a warning:
>       imap.c:638: warning: unsigned int format,
>       different type arg (arg 3)
> 
> And "man 3 sscanf" says;
>       u    Matches an unsigned decimal integer; the
>       next pointer must be a pointer to unsigned int.

Correct, sscanf() only cares about sizeof(unsigned int) part of
variable whose address is passed.

                        :
> shoud be something like this:
> 
> |#ifdef USE_HCACHE
> |    /* save UIDVALIDITY for the header cache */
> |    else if (ascii_strncasecmp("OK [UIDVALIDITY", pc, 14) == 0)
> |    {
> +      unsigned int *tmp_uid_val;
> |      dprint(2, (debugfile, "Getting mailbox UIDVALIDITY\n"));
> |      pc += 3;
> |      pc = imap_next_word(pc);
> !      sscanf(pc, "%u", tmp_uid_val);
> +      idata->uid_validity = *tmp_uid_val;
> |    }
> |#endif

I think you meant something like this:
  unsigned int validity = 0;
        :
  sscanf(pc, "%u", &validity);
  idata->uid_validity = validity;

i.e., the address passed to sscanf() must actually point to a storage
enough to hold an unsigned-integer value.
And it's always good to check the return value from a function if any.
It can fail if the passed string does not represent an unsigned integer
(and not even sure if sscanf() doesn't touch `validity' when it failed):
  unsigned int validity = 0;
        :
  if (sscanf(pc, "%u", &validity) == 1)
      idata->uid_validity = validity;
  else
      /* abort parsing any further; we can't trust what the server says */