Skip to content

Commit 2447411

Browse files
authored
libbpf-tools/syscount: Fix incorrect errno usage (#4991)
In read_vals_batch function, the errno set by bpf_map_lookup_and_delete_batch() may change if the next warn() fails. So the errno of read_vals:240 may be incorrect. bpf_map_lookup_and_delete_batch() already returns errno with a minus added on failure, so used the return value instead of errno.
1 parent 1150523 commit 2447411

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

libbpf-tools/syscount.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ static void print_timestamp()
191191

192192
static bool batch_map_ops = true; /* hope for the best */
193193

194-
static bool read_vals_batch(int fd, struct data_ext_t *vals, __u32 *count)
194+
static int read_vals_batch(int fd, struct data_ext_t *vals, __u32 *count)
195195
{
196196
struct data_t orig_vals[*count];
197197
void *in = NULL, *out;
@@ -203,13 +203,13 @@ static bool read_vals_batch(int fd, struct data_ext_t *vals, __u32 *count)
203203
n = *count - n_read;
204204
err = bpf_map_lookup_and_delete_batch(fd, &in, &out,
205205
keys + n_read, orig_vals + n_read, &n, NULL);
206-
if (err && errno != ENOENT) {
206+
if (err < 0 && err != -ENOENT) {
207207
/* we want to propagate EINVAL upper, so that
208208
* the batch_map_ops flag is set to false */
209-
if (errno != EINVAL)
209+
if (err != -EINVAL)
210210
warn("bpf_map_lookup_and_delete_batch: %s\n",
211211
strerror(-err));
212-
return false;
212+
return err;
213213
}
214214
n_read += n;
215215
in = out;
@@ -223,7 +223,7 @@ static bool read_vals_batch(int fd, struct data_ext_t *vals, __u32 *count)
223223
}
224224

225225
*count = n_read;
226-
return true;
226+
return 0;
227227
}
228228

229229
static bool read_vals(int fd, struct data_ext_t *vals, __u32 *count)
@@ -236,12 +236,12 @@ static bool read_vals(int fd, struct data_ext_t *vals, __u32 *count)
236236
int err;
237237

238238
if (batch_map_ops) {
239-
bool ok = read_vals_batch(fd, vals, count);
240-
if (!ok && errno == EINVAL) {
239+
err = read_vals_batch(fd, vals, count);
240+
if (err < 0 && err == -EINVAL) {
241241
/* fall back to a racy variant */
242242
batch_map_ops = false;
243243
} else {
244-
return ok;
244+
return err >= 0;
245245
}
246246
}
247247

0 commit comments

Comments
 (0)