[PATCH] D54527: Add new interceptor for strtonum(3)

Kamil Rytarowski via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 14 07:54:10 PST 2018


krytarowski created this revision.
krytarowski added reviewers: joerg, vitalybuka.
krytarowski added a project: Sanitizers.
Herald added subscribers: llvm-commits, kubamracek.

strtonum(3) reliably convertss string value to an integer.
This function is used in OpenBSD compat namespace
and is located inside NetBSD's libc.

Add a dedicated test for this interface.


Repository:
  rL LLVM

https://reviews.llvm.org/D54527

Files:
  lib/sanitizer_common/sanitizer_common_interceptors.inc
  lib/sanitizer_common/sanitizer_platform_interceptors.h
  test/sanitizer_common/TestCases/NetBSD/strtonum.cc


Index: test/sanitizer_common/TestCases/NetBSD/strtonum.cc
===================================================================
--- /dev/null
+++ test/sanitizer_common/TestCases/NetBSD/strtonum.cc
@@ -0,0 +1,39 @@
+// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
+
+#define _OPENBSD_SOURCE
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <err.h>
+
+int main(void) {
+  long long l;
+  const char *errstr;
+
+  l = strtonum("100", 1, 100, &errstr);
+  if (errstr)
+    errx(1, "strtonum: %s", errstr);
+  printf("%lld\n", l);
+
+  l = strtonum("200", 1, 100, &errstr);
+  if (!errstr)
+    errx(1, "strtonum");
+  printf("%s\n", errstr);
+
+  l = strtonum("300", 1000, 1001, &errstr);
+  if (!errstr)
+    errx(1, "strtonum");
+  printf("%s\n", errstr);
+
+  l = strtonum("abc", 1000, 1001, &errstr);
+  if (!errstr)
+    errx(1, "strtonum");
+  printf("%s\n", errstr);
+
+  // CHECK: 100
+  // CHECK: too large
+  // CHECK: too small
+  // CHECK: invalid
+
+  return 0;
+}
Index: lib/sanitizer_common/sanitizer_platform_interceptors.h
===================================================================
--- lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ lib/sanitizer_common/sanitizer_platform_interceptors.h
@@ -516,5 +516,6 @@
 #define SANITIZER_INTERCEPT_TTYENT SI_NETBSD
 #define SANITIZER_INTERCEPT_PROTOENT SI_NETBSD
 #define SANITIZER_INTERCEPT_NETENT SI_NETBSD
+#define SANITIZER_INTERCEPT_STRTONUM SI_NETBSD
 
 #endif  // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H
Index: lib/sanitizer_common/sanitizer_common_interceptors.inc
===================================================================
--- lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -7264,6 +7264,26 @@
 #define INIT_NETENT
 #endif
 
+#if SANITIZER_INTERCEPT_STRTONUM
+INTERCEPTOR(long long, strtonum, const char *nptr, long long minval,
+            long long maxval, const char **errstr) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, strtonum, nptr, minval, maxval, errstr);
+  if (nptr)
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, nptr, REAL(strlen)(nptr) + 1);
+  long long ret = REAL(strtonum)(nptr, minval, maxval, errstr);
+  if (errstr) {
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errstr, sizeof(const char *));
+    if (*errstr)
+      COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *errstr, REAL(strlen)(*errstr) + 1);
+  }
+  return ret;
+}
+#define INIT_STRTONUM COMMON_INTERCEPT_FUNCTION(strtonum)
+#else
+#define INIT_STRTONUM
+#endif
+
 static void InitializeCommonInterceptors() {
   static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
   interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
@@ -7517,6 +7537,7 @@
   INIT_TTYENT;
   INIT_PROTOENT;
   INIT_NETENT;
+  INIT_STRTONUM;
 
   INIT___PRINTF_CHK;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54527.174042.patch
Type: text/x-patch
Size: 2820 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181114/91c6cadf/attachment.bin>


More information about the llvm-commits mailing list