Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf header: Make topology checkers to check return value of strbuf

Make topology checkers to check the return value of strbuf APIs so that
it can detect errors in it.

Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/20160510054735.6158.98650.stgit@devbox
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Masami Hiramatsu and committed by
Arnaldo Carvalho de Melo
642aadaa 70a6898f

+20 -11
+20 -11
tools/perf/util/header.c
··· 1819 1819 1820 1820 ph->env.nr_sibling_cores = nr; 1821 1821 size += sizeof(u32); 1822 - strbuf_init(&sb, 128); 1822 + if (strbuf_init(&sb, 128) < 0) 1823 + goto free_cpu; 1823 1824 1824 1825 for (i = 0; i < nr; i++) { 1825 1826 str = do_read_string(fd, ph); ··· 1828 1827 goto error; 1829 1828 1830 1829 /* include a NULL character at the end */ 1831 - strbuf_add(&sb, str, strlen(str) + 1); 1830 + if (strbuf_add(&sb, str, strlen(str) + 1) < 0) 1831 + goto error; 1832 1832 size += string_size(str); 1833 1833 free(str); 1834 1834 } ··· 1851 1849 goto error; 1852 1850 1853 1851 /* include a NULL character at the end */ 1854 - strbuf_add(&sb, str, strlen(str) + 1); 1852 + if (strbuf_add(&sb, str, strlen(str) + 1) < 0) 1853 + goto error; 1855 1854 size += string_size(str); 1856 1855 free(str); 1857 1856 } ··· 1915 1912 /* nr nodes */ 1916 1913 ret = readn(fd, &nr, sizeof(nr)); 1917 1914 if (ret != sizeof(nr)) 1918 - goto error; 1915 + return -1; 1919 1916 1920 1917 if (ph->needs_swap) 1921 1918 nr = bswap_32(nr); 1922 1919 1923 1920 ph->env.nr_numa_nodes = nr; 1924 - strbuf_init(&sb, 256); 1921 + if (strbuf_init(&sb, 256) < 0) 1922 + return -1; 1925 1923 1926 1924 for (i = 0; i < nr; i++) { 1927 1925 /* node number */ ··· 1944 1940 mem_free = bswap_64(mem_free); 1945 1941 } 1946 1942 1947 - strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":", 1948 - node, mem_total, mem_free); 1943 + if (strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":", 1944 + node, mem_total, mem_free) < 0) 1945 + goto error; 1949 1946 1950 1947 str = do_read_string(fd, ph); 1951 1948 if (!str) 1952 1949 goto error; 1953 1950 1954 1951 /* include a NULL character at the end */ 1955 - strbuf_add(&sb, str, strlen(str) + 1); 1952 + if (strbuf_add(&sb, str, strlen(str) + 1) < 0) 1953 + goto error; 1956 1954 free(str); 1957 1955 } 1958 1956 ph->env.numa_nodes = strbuf_detach(&sb, NULL); ··· 1988 1982 } 1989 1983 1990 1984 ph->env.nr_pmu_mappings = pmu_num; 1991 - strbuf_init(&sb, 128); 1985 + if (strbuf_init(&sb, 128) < 0) 1986 + return -1; 1992 1987 1993 1988 while (pmu_num) { 1994 1989 if (readn(fd, &type, sizeof(type)) != sizeof(type)) ··· 2001 1994 if (!name) 2002 1995 goto error; 2003 1996 2004 - strbuf_addf(&sb, "%u:%s", type, name); 1997 + if (strbuf_addf(&sb, "%u:%s", type, name) < 0) 1998 + goto error; 2005 1999 /* include a NULL character at the end */ 2006 - strbuf_add(&sb, "", 1); 2000 + if (strbuf_add(&sb, "", 1) < 0) 2001 + goto error; 2007 2002 2008 2003 if (!strcmp(name, "msr")) 2009 2004 ph->env.msr_pmu_type = type;