From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Daniel Micay <danielmicay@gmail.com>
Date: Wed, 23 Aug 2017 20:28:03 -0400
Subject: [PATCH] use -fwrapv when signed overflow checking is off

---
 cc/cc.go       |  2 ++
 cc/sanitize.go | 12 ++++++++++++
 2 files changed, 14 insertions(+)

diff --git a/cc/cc.go b/cc/cc.go
index 31b381a9d..4ca8e847a 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -1478,6 +1478,8 @@ func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
 	}
 	if c.sanitize != nil {
 		flags = c.sanitize.flags(ctx, flags)
+	} else {
+		flags.Local.CFlags = append(flags.Local.CFlags, "-fwrapv")
 	}
 	if c.coverage != nil {
 		flags, deps = c.coverage.flags(ctx, flags, deps)
diff --git a/cc/sanitize.go b/cc/sanitize.go
index 463a02ac2..7c92b41dc 100644
--- a/cc/sanitize.go
+++ b/cc/sanitize.go
@@ -458,9 +458,21 @@ func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
 	}
 
 	if !sanitize.Properties.SanitizerEnabled && !sanitize.Properties.UbsanRuntimeDep {
+		flags.Local.CFlags = append(flags.Local.CFlags, "-fwrapv")
 		return flags
 	}
 
+        wrapv := true
+        for _, element := range sanitize.Properties.Sanitizers {
+                if (element == "signed-integer-overflow" || element == "integer" || element == "undefined") {
+                        wrapv = false
+                        break
+                }
+        }
+        if wrapv {
+                flags.Local.CFlags = append(flags.Local.CFlags, "-fwrapv")
+        }
+
 	if Bool(sanitize.Properties.Sanitize.Address) {
 		if ctx.Arch().ArchType == android.Arm {
 			// Frame pointer based unwinder in ASan requires ARM frame setup.