From: Jeff Garzik <jeff@garzik.org>
To: Colin McCabe <cmccabe@alumni.cmu.edu>
Cc: Project Hail List <hail-devel@vger.kernel.org>,
Pete Zaitcev <zaitcev@redhat.com>
Subject: Re: [PATCH v2 2/2] cld: read the cld.port file using g_file_get_contents
Date: Sat, 28 Nov 2009 05:37:16 -0500 [thread overview]
Message-ID: <4B10FD5C.8040705@garzik.org> (raw)
In-Reply-To: <1259375029-22050-1-git-send-email-cmccabe@alumni.cmu.edu>
On 11/27/2009 09:23 PM, Colin McCabe wrote:
> Signed-off-by: Colin McCabe<cmccabe@alumni.cmu.edu>
>
> ---
> lib/common.c | 29 +++++++++++++++--------------
> 1 files changed, 15 insertions(+), 14 deletions(-)
>
> diff --git a/lib/common.c b/lib/common.c
> index 68f60f8..db20e2a 100644
> --- a/lib/common.c
> +++ b/lib/common.c
> @@ -1,4 +1,5 @@
>
> +#include<stdio.h>
> #include<stdlib.h>
> #include<fcntl.h>
> #include<unistd.h>
> @@ -61,26 +62,26 @@ const char *cld_errstr(enum cle_err_codes ecode)
> */
> int cld_readport(const char *fname)
> {
> - enum { LEN = 11 };
> - char buf[LEN+1];
> long port;
> - int fd;
> - int rc;
> + gchar *buf;
> + GError *err = NULL;
> + gsize len;
>
> - if ((fd = open(fname, O_RDONLY)) == -1)
> - return -errno;
> - rc = read(fd, buf, LEN);
> - close(fd);
> - if (rc< 0)
> - return -errno;
> - if (rc == 0)
> - return -EPIPE;
> - buf[rc] = 0;
> + if (!g_file_get_contents(fname,&buf,&len,&err)) {
> + fprintf(stderr, "Unable to read port file: %s\n",
> + err->message);
> + g_error_free(err);
> + return -EIO;
> + }
>
> + if (len == 0) {
> + g_free(buf);
> + return -EPIPE;
> + }
> port = strtol(buf, NULL, 10);
> + g_free(buf);
Two added wrinkles, unfortunately:
1) 'buf' is no longer nul-terminated, which means strtol() has become a
buffer overrun.
2) this is used in daemons, so we cannot make assumptions about error
output. The best you can do is return a useful numeric error code,
because fprintf(stderr,...) does nothing in a daemon that redirected
stderr to null.
next prev parent reply other threads:[~2009-11-28 10:37 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-11-28 2:23 [PATCH v2 2/2] cld: read the cld.port file using g_file_get_contents Colin McCabe
2009-11-28 10:37 ` Jeff Garzik [this message]
2009-11-28 10:49 ` Jeff Garzik
2009-11-28 17:58 ` Pete Zaitcev
2009-11-28 21:23 ` Jeff Garzik
2009-11-28 23:02 ` Pete Zaitcev
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4B10FD5C.8040705@garzik.org \
--to=jeff@garzik.org \
--cc=cmccabe@alumni.cmu.edu \
--cc=hail-devel@vger.kernel.org \
--cc=zaitcev@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.