[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <1385135039.1747.118.camel@leonhard>
Date: Sat, 23 Nov 2013 00:43:59 +0900
From: Namhyung Kim <namhyung@...nel.org>
To: Jiri Olsa <jolsa@...hat.com>
Cc: linux-kernel@...r.kernel.org,
Corey Ashford <cjashfor@...ux.vnet.ibm.com>,
Frederic Weisbecker <fweisbec@...il.com>,
Ingo Molnar <mingo@...e.hu>, Paul Mackerras <paulus@...ba.org>,
Peter Zijlstra <a.p.zijlstra@...llo.nl>,
Arnaldo Carvalho de Melo <acme@...hat.com>,
Steven Rostedt <rostedt@...dmis.org>,
David Ahern <dsahern@...il.com>
Subject: Re: [PATCH 19/22] perf tools: Add filename__read_str util function
2013-11-21 (목), 12:01 +0100, Jiri Olsa:
> Adding filename__read_str util function to read
> text file and return it in the char array.
>
> The interface is:
> int filename__read_str(const char *filename, char **buf, size_t *sizep)
>
> Returns 0/-1 if the read suceeded/fail respectively.
>
> buf - place to store the data pointer
> size - place to store data size
[SNIP]
> +int filename__read_str(const char *filename, char **buf, size_t *sizep)
> +{
> + size_t size = 0, alloc_size = 0;
> + void *bf = NULL, *nbf;
> + int fd, n, err = 0;
> +
> + fd = open(filename, O_RDONLY);
> + if (fd < 0)
> + return -errno;
> +
> + do {
> + if (size == alloc_size) {
> + alloc_size += BUFSIZ;
> + nbf = realloc(bf, alloc_size);
> + if (!nbf) {
> + err = -ENOMEM;
> + break;
> + }
> +
> + bf = nbf;
> + }
> +
> + n = read(fd, bf + size, BUFSIZ);
Shouldn't it be "read(fd, bf + size, alloc_size - size)"?
Otherwise there might be a problem if read() returned early for some
reason with small size and then retry with a full BUFSIZ..
> + if (n < 0) {
> + err = 0;
I think it needs to check the size also since read() might fail at the
first invocation. What about this?
if (n < 0) {
if (size)
err = 0;
else
err = -errno;
Thanks,
Namhyung
> + break;
> + }
> +
> + size += n;
> + } while (n > 0);
> +
> + if (!err) {
> + *sizep = size;
> + *buf = bf;
> + } else
> + free(bf);
> +
> + close(fd);
> + return err;
> +}
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists