Skip to content

eBPF-based security platform

bpm: parse version and tree numbers within their text

Daniel Borkmannefaabe5632af
diff --git a/util/bpm_package.c b/util/bpm_package.c
index 9bef138e..e0327b99 100644
--- a/util/bpm_package.c
+++ b/util/bpm_package.c
@@ -20,6 +20,7 @@
#include <openssl/x509.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "bpm_oci.h"
#include "bpm_package.h"
@@ -157,17 +158,17 @@ int bpm_semver_compare(const char *a, const char *b)
const char *pa = a, *pb = b;
for (int part = 0; part < 3; part++) {
- char *ea, *eb;
+ size_t na = shirudo_digits(pa, strlen(pa));
+ size_t nb = shirudo_digits(pb, strlen(pb));
__u64 va, vb;
- va = strtoul(pa, &ea, 10);
- vb = strtoul(pb, &eb, 10);
- if (ea == pa || eb == pb)
+ if (parse_u64(pa, na, UINT64_MAX, &va) ||
+ parse_u64(pb, nb, UINT64_MAX, &vb))
return strcmp(a, b);
if (va != vb)
return va > vb ? 1 : -1;
- pa = *ea == '.' ? ea + 1 : ea;
- pb = *eb == '.' ? eb + 1 : eb;
+ pa = pa[na] == '.' ? pa + na + 1 : pa + na;
+ pb = pb[nb] == '.' ? pb + nb + 1 : pb + nb;
}
if (*pa == '-' || *pb == '-') {
if (*pa != '-' || *pb != '-')
diff --git a/util/bpm_tree.c b/util/bpm_tree.c
index 9cb3ee80..098adfe9 100644
--- a/util/bpm_tree.c
+++ b/util/bpm_tree.c
@@ -9,6 +9,7 @@
#include <string.h>
#include <shirudo/misc.h>
+#include <shirudo/string.h>
#include "bpm_tree.h"
#include "crypto.h"
@@ -164,33 +165,16 @@ static int tp_skip(struct tree_parser *p)
static int tp_int(struct tree_parser *p, __s64 *out)
{
- const char *start, *scan;
- __u64 value = 0, limit;
- __u8 digit;
- bool negative = false;
+ size_t len, n;
+ bool neg;
tp_ws(p);
- start = p->p;
- scan = start;
- if (scan < p->end && *scan == '-') {
- negative = true;
- scan++;
- }
- if (scan >= p->end || !isdigit((__u8)*scan))
+ len = (size_t)(p->end - p->p);
+ neg = len && *p->p == '-';
+ n = neg + shirudo_digits(p->p + neg, len - neg);
+ if (parse_s64(p->p, n, INT64_MIN, INT64_MAX, out))
return -EINVAL;
- limit = negative ? (__u64)INT64_MAX + 1 : INT64_MAX;
- while (scan < p->end && isdigit((__u8)*scan)) {
- digit = (__u8)*scan - '0';
- if (value > (limit - digit) / 10)
- return -EINVAL;
- value = value * 10 + digit;
- scan++;
- }
- p->p = scan;
- if (negative && value == (__u64)INT64_MAX + 1)
- *out = INT64_MIN;
- else
- *out = negative ? -(__s64)value : (__s64)value;
+ p->p += n;
return 0;
}