Fix parsing of unsigned constants.
We were using strtol(3) so unsigned constants that wouldn't fit in a
signed int were not parsed correctly.
Fix by using strtoul(3) on signed overflow.
Bug: crbug.com/657508
Test: syscall_filter_unittest
Change-Id: I7a986cf6c5f3a84dbd8605861794e9d7ef9c4ce7
diff --git a/syscall_filter_unittest.cpp b/syscall_filter_unittest.cpp
index 2445f0a..c94aa24 100644
--- a/syscall_filter_unittest.cpp
+++ b/syscall_filter_unittest.cpp
@@ -20,12 +20,76 @@
#include <fcntl.h> /* For O_WRONLY. */
#include <gtest/gtest.h>
+#include <string>
#include "bpf.h"
#include "syscall_filter.h"
#include "syscall_filter_unittest_macros.h"
#include "util.h"
+TEST(util, parse_constant_unsigned) {
+ char *end;
+ long int c = 0;
+ std::string constant;
+
+#if defined(BITS32)
+ constant = "0x80000000";
+ c = parse_constant(const_cast<char*>(constant.c_str()), &end);
+ EXPECT_EQ(0x80000000U, static_cast<unsigned long int>(c));
+
+#elif defined(BITS64)
+ constant = "0x8000000000000000";
+ c = parse_constant(const_cast<char*>(constant.c_str()), &end);
+ EXPECT_EQ(0x8000000000000000UL, static_cast<unsigned long int>(c));
+#endif
+}
+
+TEST(util, parse_constant_unsigned_toobig) {
+ char *end;
+ long int c = 0;
+ std::string constant;
+
+#if defined(BITS32)
+ constant = "0x100000000"; // Too big for 32-bit unsigned long int.
+ c = parse_constant(const_cast<char*>(constant.c_str()), &end);
+ // Error case should return 0.
+ EXPECT_EQ(0, c);
+
+#elif defined(BITS64)
+ constant = "0x10000000000000000";
+ c = parse_constant(const_cast<char*>(constant.c_str()), &end);
+ // Error case should return 0.
+ EXPECT_EQ(0, c);
+#endif
+}
+
+TEST(util, parse_constant_signed) {
+ char *end;
+ long int c = 0;
+ std::string constant = "-1";
+ c = parse_constant(const_cast<char*>(constant.c_str()), &end);
+ EXPECT_EQ(-1, c);
+}
+
+TEST(util, parse_constant_signed_toonegative) {
+ char *end;
+ long int c = 0;
+ std::string constant;
+
+#if defined(BITS32)
+ constant = "-0x80000001";
+ c = parse_constant(const_cast<char*>(constant.c_str()), &end);
+ // Error case should return 0.
+ EXPECT_EQ(0, c);
+
+#elif defined(BITS64)
+ constant = "-0x8000000000000001";
+ c = parse_constant(const_cast<char*>(constant.c_str()), &end);
+ // Error case should return 0.
+ EXPECT_EQ(0, c);
+#endif
+}
+
/* Test that setting one BPF instruction works. */
TEST(bpf, set_bpf_instr) {
struct sock_filter instr;