git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Type mismatches in safe_read and friends?

From
RDRogan Dawes <lists@dawes.za.net>
Date
Mar 26, 2007, 14:13 UTC
Message-ID
<4607D4F6.80703@dawes.za.net>
Hi folks,

I'm starting to learn a little C, and I figured I'd learn from the masters ;-) I needed to read in some data from the network, and I figured the safe_* calls would be a good example of how to do it correctly.

So, I took a look, and found:
static void safe_read(int fd, void *buffer, unsigned size)
{
         int n = 0;
         while (n < size) {
                 int ret = xread(fd, (char *) buffer + n, size - n);
                 if (ret < 0)
                         die("read error (%s)", strerror(errno));
                 if (!ret)
                         die("The remote end hung up unexpectedly");
                 n += ret;
         }
}
Surely size and 'n' should have the same signed-ness?

And, in fact, shouldn't they actually be size_t, rather than 'int', since xread is defined as:

static inline ssize_t xread(int fd, void *buf, size_t len)
{
         ssize_t nr;
         while (1) {
                 nr = read(fd, buf, len);
                 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
                         continue;
                 return nr;
         }
}
And finally, 'ret' in safe_read should be a 'ssize_t', not an int, right?

Or is it just a case that we don't really care, since we control the ranges of the values, and the underlying types are int anyway? Patches to follow if I get an indication that anyone cares, otherwise I'd be posting my question to a C newbies group. ;-)

Rogan
Next: Shawn O. Pearce
Message 1 of 3 in “Type mismatches in safe_read and friends?”
  1. Rogan DawesMar 26, 2007
  2. Shawn O. PearceMar 26, 2007
  3. Rogan DawesMar 27, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.