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 983ffc0e8..45a50cfdf 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -533,6 +533,8 @@ func (c *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
 	}
 	if c.sanitize != nil {
 		flags = c.sanitize.flags(ctx, flags)
+	} else {
+		flags.CFlags = append(flags.CFlags, "-fwrapv")
 	}
 	if c.coverage != nil {
 		flags = c.coverage.flags(ctx, flags)
diff --git a/cc/sanitize.go b/cc/sanitize.go
index 9c3b8e529..1dbd9fe11 100644
--- a/cc/sanitize.go
+++ b/cc/sanitize.go
@@ -271,6 +271,7 @@ func (sanitize *sanitize) deps(ctx BaseModuleContext, deps Deps) Deps {
 
 func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
 	if !sanitize.Properties.SanitizerEnabled {
+		flags.CFlags = append(flags.CFlags, "-fwrapv")
 		return flags
 	}
 
@@ -315,6 +316,17 @@ func (sanitize *sanitize) flags(ctx ModuleContext, flags Flags) Flags {
 		sanitizers = append(sanitizers, sanitize.Properties.Sanitize.Misc_undefined...)
 	}
 
+	wrapv := true
+	for _, element := range sanitizers {
+		if (element == "signed-integer-overflow" || element == "integer" || element == "undefined") {
+			wrapv = false
+			break
+		}
+	}
+	if wrapv {
+		flags.CFlags = append(flags.CFlags, "-fwrapv")
+	}
+
 	if Bool(sanitize.Properties.Sanitize.Diag.Undefined) {
 		diagSanitizers = append(diagSanitizers, "undefined")
 	}