[PATCH] D54081: Add new interceptor for atof(3)

Kamil Rytarowski via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 3 14:43:40 PDT 2018


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

atof - convert ASCII string to double


Repository:
  rL LLVM

https://reviews.llvm.org/D54081

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


Index: test/sanitizer_common/TestCases/NetBSD/atof.cc
===================================================================
--- /dev/null
+++ test/sanitizer_common/TestCases/NetBSD/atof.cc
@@ -0,0 +1,15 @@
+// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(void) {
+  double d;
+
+  d = atof("3.14");
+  printf("atof() = %.2f\n", d);
+
+  // CHECK: atof() = 3.14
+
+  return 0;
+}
Index: lib/sanitizer_common/sanitizer_platform_interceptors.h
===================================================================
--- lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ lib/sanitizer_common/sanitizer_platform_interceptors.h
@@ -514,5 +514,6 @@
 #define SANITIZER_INTERCEPT_TTYENT SI_NETBSD
 #define SANITIZER_INTERCEPT_PROTOENT SI_NETBSD
 #define SANITIZER_INTERCEPT_NETENT SI_NETBSD
+#define SANITIZER_INTERCEPT_ATOF 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
@@ -7252,6 +7252,19 @@
 #define INIT_NETENT
 #endif
 
+#if SANITIZER_INTERCEPT_ATOF
+INTERCEPTOR(double, atof, char *nptr) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, atof, nptr);
+  if (nptr)
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, nptr, REAL(strlen)(nptr) + 1);
+  return REAL(atof)(nptr);
+}
+#define INIT_ATOF COMMON_INTERCEPT_FUNCTION(atof)
+#else
+#define INIT_ATOF
+#endif
+
 static void InitializeCommonInterceptors() {
   static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
   interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
@@ -7503,6 +7516,7 @@
   INIT_TTYENT;
   INIT_PROTOENT;
   INIT_NETENT;
+  INIT_ATOF;
 
   INIT___PRINTF_CHK;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54081.172506.patch
Type: text/x-patch
Size: 1897 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181103/0a6c405e/attachment.bin>


More information about the llvm-commits mailing list