+/* Example output:
+==2749== Conditional jump or move depends on uninitialised value(s)
+==2749== at 0x4026C60: strnlen (mc_replace_strmem.c:263)
+==2749== by 0x40850E3: vfprintf (vfprintf.c:1614)
+==2749== by 0x408EACF: printf (printf.c:35)
+==2749== by 0x8048465: main (in /tmp/foo)
+==2749==
+==2749== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1
+==2749== at 0x4025BD3: malloc (vg_replace_malloc.c:236)
+==2749== by 0x8048444: main (in /tmp/foo)
+==2749==
+*/
+
+static bool blank_line(const char *line)
+{
+ return line[strspn(line, "=0123456789 ")] == '\0';
+}
+
+static char *get_leaks(const char *output, char **errs)
+{
+ char *leaks = talloc_strdup(output, "");
+ unsigned int i, num;
+ char **lines = strsplit(output, output, "\n", &num);
+
+ *errs = talloc_strdup(output, "");
+ for (i = 0; i < num; i++) {
+ if (strstr(lines[i], " lost ")) {
+ /* A leak... */
+ if (strstr(lines[i], " definitely lost ")) {
+ /* Definite leak, report. */
+ while (lines[i] && !blank_line(lines[i])) {
+ leaks = talloc_append_string(leaks,
+ lines[i]);
+ leaks = talloc_append_string(leaks,
+ "\n");
+ i++;
+ }
+ } else
+ /* Not definite, ignore. */
+ while (lines[i] && !blank_line(lines[i]))
+ i++;
+ } else {
+ /* A real error. */
+ while (lines[i] && !blank_line(lines[i])) {
+ *errs = talloc_append_string(*errs, lines[i]);
+ *errs = talloc_append_string(*errs, "\n");
+ i++;
+ }
+ }
+ }
+ if (!leaks[0]) {
+ talloc_free(leaks);
+ leaks = NULL;
+ }
+ if (!(*errs)[0]) {
+ talloc_free(*errs);
+ *errs = NULL;
+ }
+ return leaks;
+}
+