From 6d3c80b85a99fe5e6c76c8d6512eddf2edab3c86 Mon Sep 17 00:00:00 2001 From: Nick Kralevich Date: Wed, 27 Mar 2019 07:46:39 -0700 Subject: [PATCH] zip_writer.cc: update powerof2 and use it unconditionally Update the powerof2 macro to one that is integer overflow safe. This definition is the same one used by bionic, but cleaned up using clang_format. Unconditionally use this powerof2 macro, to ensure consistency across all the build environments. Test: compiles and boots Change-Id: I21bfaf2360d1f77caf96365e20490b1deadfdfe0 --- libziparchive/zip_writer.cc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libziparchive/zip_writer.cc b/libziparchive/zip_writer.cc index 981df3add..0df0fa538 100644 --- a/libziparchive/zip_writer.cc +++ b/libziparchive/zip_writer.cc @@ -30,9 +30,13 @@ #include "entry_name_utils-inl.h" #include "zip_archive_common.h" -#if !defined(powerof2) -#define powerof2(x) ((((x)-1) & (x)) == 0) -#endif +#undef powerof2 +#define powerof2(x) \ + ({ \ + __typeof__(x) _x = (x); \ + __typeof__(x) _x2; \ + __builtin_add_overflow(_x, -1, &_x2) ? 1 : ((_x2 & _x) == 0); \ + }) /* Zip compression methods we support */ enum {