Conflicts: Documentation/ABI/testing/sysfs-block-zram Documentation/ABI/testing/sysfs-class-power Documentation/blockdev/zram.txt Documentation/devicetree/bindings/arm/coresight.txt Documentation/devicetree/bindings/i2c/qcom,i2c-qcom-geni.txt Makefile arch/arm/Makefile arch/arm/configs/vendor/sa515m-perf_defconfig arch/arm/configs/vendor/sa515m_defconfig arch/arm/configs/vendor/sdxprairie-perf_defconfig arch/arm/configs/vendor/sdxprairie_defconfig arch/arm/kernel/smp.c arch/arm64/boot/dts/qcom/Makefile arch/arm64/boot/dts/qcom/atoll-idp.dtsi arch/arm64/boot/dts/qcom/atoll-sde-display.dtsi arch/arm64/boot/dts/qcom/sa515m-flashless.dtsi arch/arm64/boot/dts/qcom/sa8155-adp-star-display.dtsi arch/arm64/boot/dts/qcom/sdxprairie-cdp.dtsi arch/arm64/boot/dts/qcom/sdxprairie-mtp-le-cpe.dtsi arch/arm64/boot/dts/qcom/sdxprairie-v2-mtp-le-cpe.dtsi arch/arm64/boot/dts/qcom/sdxprairie.dtsi arch/arm64/boot/dts/qcom/sm8150-sdxprairie.dtsi arch/arm64/boot/dts/qcom/sm8150.dtsi arch/arm64/configs/cuttlefish_defconfig arch/arm64/include/asm/alternative.h arch/arm64/include/asm/cpucaps.h arch/arm64/include/asm/pgtable.h arch/arm64/include/asm/processor.h arch/arm64/include/asm/ptrace.h arch/arm64/include/asm/sysreg.h arch/arm64/kernel/cpu_errata.c arch/arm64/kernel/cpufeature.c arch/arm64/kernel/smp.c arch/arm64/kernel/vdso/Makefile arch/arm64/kernel/vmlinux.lds.S arch/arm64/mm/init.c arch/x86/configs/x86_64_cuttlefish_defconfig arch/x86/kernel/vmlinux.lds.S block/bio.c block/blk-core.c block/blk-merge.c build.config.common build.config.cuttlefish.aarch64 build.config.cuttlefish.x86_64 drivers/Makefile drivers/android/binder.c drivers/base/power/main.c drivers/base/power/wakeup.c drivers/block/virtio_blk.c drivers/block/zram/zram_drv.c drivers/block/zram/zram_drv.h drivers/bus/mhi/core/mhi_init.c drivers/bus/mhi/core/mhi_internal.h drivers/bus/mhi/core/mhi_main.c drivers/bus/mhi/core/mhi_pm.c drivers/char/adsprpc.c drivers/char/diag/diagchar.h drivers/clk/clk.c drivers/clk/qcom/mdss/mdss-dsi-pll-7nm.c drivers/cpufreq/cpufreq_stats.c drivers/crypto/msm/qce50.c drivers/dma-buf/dma-buf.c drivers/gpu/drm/msm/dsi-staging/dsi_defs.h drivers/gpu/drm/msm/dsi-staging/dsi_display.c drivers/gpu/drm/msm/dsi-staging/dsi_drm.c drivers/gpu/drm/msm/dsi-staging/dsi_panel.c drivers/gpu/drm/msm/dsi-staging/dsi_panel.h drivers/gpu/drm/msm/msm_gem.c drivers/gpu/drm/msm/msm_kms.h drivers/gpu/drm/msm/sde/sde_connector.c drivers/gpu/drm/msm/sde/sde_encoder.c drivers/gpu/msm/adreno_a5xx.h drivers/gpu/msm/adreno_a5xx_preempt.c drivers/gpu/msm/adreno_a6xx_gmu.c drivers/gpu/msm/adreno_ringbuffer.c drivers/gpu/msm/kgsl.c drivers/gpu/msm/kgsl.h drivers/gpu/msm/kgsl_debugfs.c drivers/gpu/msm/kgsl_device.h drivers/gpu/msm/kgsl_mmu.c drivers/gpu/msm/kgsl_pool.c drivers/gpu/msm/kgsl_trace.h drivers/hid/hid-qvr.c drivers/hwtracing/coresight/Makefile drivers/hwtracing/coresight/coresight-byte-cntr.c drivers/hwtracing/coresight/coresight-etm-perf.c drivers/hwtracing/coresight/coresight-etm4x.c drivers/hwtracing/coresight/coresight-funnel.c drivers/hwtracing/coresight/coresight-tmc-etf.c drivers/hwtracing/coresight/coresight-tmc-etr.c drivers/hwtracing/coresight/coresight-tmc.c drivers/hwtracing/coresight/coresight-tmc.h drivers/hwtracing/coresight/coresight.c drivers/hwtracing/coresight/of_coresight.c drivers/i2c/busses/i2c-qcom-geni.c drivers/input/misc/qpnp-power-on.c drivers/irqchip/irq-gic-v3.c drivers/md/Kconfig drivers/md/dm-default-key.c drivers/media/platform/msm/ais/cam_isp/isp_hw_mgr/cam_ife_hw_mgr.c drivers/media/platform/msm/ais/cam_sensor_module/cam_cci/cam_cci_core.c drivers/media/platform/msm/ais/cam_sensor_module/cam_sensor_io/cam_sensor_cci_i2c.c drivers/media/platform/msm/camera/cam_core/cam_hw_mgr_intf.h drivers/media/platform/msm/camera/cam_icp/icp_hw/icp_hw_mgr/cam_icp_hw_mgr.c drivers/media/platform/msm/camera/cam_isp/cam_isp_context.c drivers/media/platform/msm/camera/cam_isp/isp_hw_mgr/cam_ife_hw_mgr.c drivers/media/platform/msm/camera/cam_isp/isp_hw_mgr/isp_hw/ife_csid_hw/cam_ife_csid_core.c drivers/media/platform/msm/camera/cam_lrme/lrme_hw_mgr/cam_lrme_hw_mgr.c drivers/media/platform/msm/camera/cam_req_mgr/cam_req_mgr_core.c drivers/media/platform/msm/camera/cam_sensor_module/cam_cci/cam_cci_dev.h drivers/media/platform/msm/camera/cam_sensor_module/cam_csiphy/include/cam_csiphy_1_2_2_hwreg.h drivers/media/platform/msm/camera/cam_smmu/cam_smmu_api.c drivers/media/platform/msm/npu_v2/npu_mgr.c drivers/mmc/core/quirks.h drivers/mmc/core/sdio_irq.c drivers/mmc/host/sdhci-msm.c drivers/mtd/devices/msm_qpic_nand.c drivers/net/ethernet/aquantia/atlantic-fwd/atl_common.h drivers/net/ethernet/aquantia/atlantic-fwd/atl_ethtool.c drivers/net/ethernet/aquantia/atlantic-fwd/atl_fw.c drivers/net/ethernet/aquantia/atlantic-fwd/atl_fw.h drivers/net/ethernet/aquantia/atlantic-fwd/atl_hw.c drivers/net/ethernet/aquantia/atlantic-fwd/atl_hw.h drivers/net/ethernet/aquantia/atlantic-fwd/atl_macsec.c drivers/net/ethernet/aquantia/atlantic-fwd/atl_main.c drivers/net/ethernet/aquantia/atlantic-fwd/macsec/macsec_api.c drivers/net/ethernet/aquantia/atlantic-fwd/macsec/macsec_api.h drivers/net/ethernet/aquantia/atlantic-fwd/release_notes.txt drivers/net/ethernet/qualcomm/rmnet/rmnet_config.c drivers/net/ethernet/stmicro/stmmac/dwmac-qcom-ethqos.c drivers/net/ethernet/stmicro/stmmac/dwmac-qcom-ethqos.h drivers/net/ethernet/stmicro/stmmac/stmmac.h drivers/net/ethernet/stmicro/stmmac/stmmac_main.c drivers/net/macsec.c drivers/platform/msm/ipa/ipa_api.h drivers/platform/msm/ipa/ipa_v3/ethernet/ipa_eth.c drivers/platform/msm/ipa/ipa_v3/ethernet/ipa_eth_i.h drivers/platform/msm/ipa/ipa_v3/ethernet/ipa_eth_net.c drivers/platform/msm/ipa/ipa_v3/ipa_debugfs.c drivers/platform/msm/ipa/ipa_v3/ipa_i.h drivers/platform/msm/ipa/ipa_v3/ipa_nat.c drivers/platform/msm/ipa/ipa_v3/ipa_qmi_service.c drivers/platform/msm/ipa/ipa_v3/ipa_utils.c drivers/platform/msm/ipa/ipa_v3/rmnet_ipa_fd_ioctl.c drivers/platform/msm/qcom-geni-se.c drivers/power/supply/qcom/battery.c drivers/power/supply/qcom/qpnp-smb5.c drivers/power/supply/qcom/smb1398-charger.c drivers/power/supply/qcom/smb5-lib.c drivers/regulator/qpnp-amoled-regulator.c drivers/rpmsg/qcom_glink_native.c drivers/rpmsg/qcom_glink_smem.c drivers/scsi/ufs/Kconfig drivers/scsi/ufs/Makefile drivers/scsi/ufs/ufs-qcom.c drivers/scsi/ufs/ufshcd.c drivers/scsi/ufs/ufshcd.h drivers/soc/qcom/Kconfig drivers/soc/qcom/Makefile drivers/soc/qcom/peripheral-loader.c drivers/soc/qcom/sdx_ext_ipc.c drivers/soc/qcom/socinfo.c drivers/spi/spi-geni-qcom.c drivers/staging/android/ion/ion.c drivers/staging/android/ion/ion.h drivers/staging/android/ion/ion_page_pool.c drivers/tty/Kconfig drivers/tty/serial/msm_geni_serial.c drivers/usb/dwc3/ep0.c drivers/usb/dwc3/gadget.c fs/crypto/Makefile fs/crypto/bio.c fs/crypto/fname.c fs/crypto/fscrypt_ice.c fs/crypto/fscrypt_ice.h fs/crypto/fscrypt_private.h fs/crypto/keyring.c fs/crypto/keysetup.c fs/crypto/keysetup_v1.c fs/direct-io.c fs/ext4/Kconfig fs/ext4/inode.c fs/ext4/ioctl.c fs/ext4/move_extent.c fs/ext4/namei.c fs/ext4/page-io.c fs/ext4/readpage.c fs/ext4/super.c fs/f2fs/checkpoint.c fs/f2fs/compress.c fs/f2fs/data.c fs/f2fs/dir.c fs/f2fs/f2fs.h fs/f2fs/gc.c fs/f2fs/inode.c fs/f2fs/namei.c fs/f2fs/node.c fs/f2fs/segment.c fs/f2fs/super.c fs/incfs/Kconfig fs/incfs/Makefile fs/incfs/data_mgmt.c fs/incfs/data_mgmt.h fs/incfs/format.c fs/incfs/format.h fs/incfs/internal.h fs/incfs/main.c fs/incfs/vfs.c fs/incfs/vfs.h fs/libfs.c fs/sdcardfs/lookup.c fs/ubifs/dir.c fs/unicode/utf8-core.c fs/userfaultfd.c include/asm-generic/vmlinux.lds.h include/linux/bio.h include/linux/blk_types.h include/linux/coresight.h include/linux/dma-buf.h include/linux/fs.h include/linux/fscrypt.h include/linux/kasan.h include/linux/mmzone.h include/linux/msm_gsi.h include/linux/overflow.h include/linux/pfk.h include/linux/sched.h include/linux/wakeup_reason.h include/net/ndisc.h include/soc/qcom/socinfo.h include/uapi/linux/fscrypt.h include/uapi/linux/incrementalfs.h include/uapi/linux/input-event-codes.h include/uapi/linux/msm_ipa.h include/uapi/linux/taskstats.h include/uapi/media/cam_req_mgr.h kernel/cpu.c kernel/events/core.c kernel/irq/chip.c kernel/power/wakeup_reason.c kernel/sched/core.c kernel/sched/core_ctl.c kernel/sched/cpufreq_schedutil.c kernel/sched/deadline.c kernel/sched/fair.c kernel/sched/rt.c kernel/sched/sched.h kernel/sched/stop_task.c kernel/sched/walt.h kernel/taskstats.c kernel/time/hrtimer.c lib/Makefile lib/test_kasan.c lib/test_stackinit.c mm/Kconfig mm/filemap.c mm/kasan/report.c mm/memblock.c mm/memory.c mm/vmscan.c mm/vmstat.c net/core/skbuff.c net/ipv4/ip_output.c net/ipv4/syncookies.c net/ipv4/sysctl_net_ipv4.c net/ipv6/syncookies.c net/qrtr/qrtr.c scripts/Makefile.lib security/pfe/Kconfig security/pfe/Makefile security/pfe/pfk.c security/pfe/pfk_ext4.c security/pfe/pfk_ext4.h security/pfe/pfk_f2fs.c security/pfe/pfk_f2fs.h security/pfe/pfk_ice.c security/pfe/pfk_ice.h security/pfe/pfk_kc.c security/pfe/pfk_kc.h security/selinux/avc.c security/selinux/hooks.c security/selinux/include/security.h sound/core/compress_offload.c sound/soc/codecs/Kconfig sound/soc/codecs/Makefile techpack/.gitignore techpack/Kbuild techpack/stub/Makefile techpack/stub/include/uapi/Kbuild techpack/stub/stub.c tools/testing/selftests/filesystems/incfs/.gitignore tools/testing/selftests/filesystems/incfs/Makefile tools/testing/selftests/filesystems/incfs/incfs_test.c tools/testing/selftests/filesystems/incfs/utils.c tools/testing/selftests/filesystems/incfs/utils.h Bug: 182255618 Change-Id: Ibdfb2b8523b6c40a8e12bf1878eb54ef4b0adb04 Signed-off-by: JohnnLee <johnnlee@google.com>
311 lines
9.3 KiB
C
311 lines
9.3 KiB
C
/* SPDX-License-Identifier: GPL-2.0 OR MIT */
|
|
#ifndef __LINUX_OVERFLOW_H
|
|
#define __LINUX_OVERFLOW_H
|
|
|
|
#include <linux/compiler.h>
|
|
#include <linux/limits.h>
|
|
|
|
/*
|
|
* In the fallback code below, we need to compute the minimum and
|
|
* maximum values representable in a given type. These macros may also
|
|
* be useful elsewhere, so we provide them outside the
|
|
* COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW block.
|
|
*
|
|
* It would seem more obvious to do something like
|
|
*
|
|
* #define type_min(T) (T)(is_signed_type(T) ? (T)1 << (8*sizeof(T)-1) : 0)
|
|
* #define type_max(T) (T)(is_signed_type(T) ? ((T)1 << (8*sizeof(T)-1)) - 1 : ~(T)0)
|
|
*
|
|
* Unfortunately, the middle expressions, strictly speaking, have
|
|
* undefined behaviour, and at least some versions of gcc warn about
|
|
* the type_max expression (but not if -fsanitize=undefined is in
|
|
* effect; in that case, the warning is deferred to runtime...).
|
|
*
|
|
* The slightly excessive casting in type_min is to make sure the
|
|
* macros also produce sensible values for the exotic type _Bool. [The
|
|
* overflow checkers only almost work for _Bool, but that's
|
|
* a-feature-not-a-bug, since people shouldn't be doing arithmetic on
|
|
* _Bools. Besides, the gcc builtins don't allow _Bool* as third
|
|
* argument.]
|
|
*
|
|
* Idea stolen from
|
|
* https://mail-index.netbsd.org/tech-misc/2007/02/05/0000.html -
|
|
* credit to Christian Biere.
|
|
*/
|
|
#define is_signed_type(type) (((type)(-1)) < (type)1)
|
|
#define __type_half_max(type) ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type)))
|
|
#define type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T)))
|
|
#define type_min(T) ((T)((T)-type_max(T)-(T)1))
|
|
|
|
|
|
#ifdef COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW
|
|
/*
|
|
* For simplicity and code hygiene, the fallback code below insists on
|
|
* a, b and *d having the same type (similar to the min() and max()
|
|
* macros), whereas gcc's type-generic overflow checkers accept
|
|
* different types. Hence we don't just make check_add_overflow an
|
|
* alias for __builtin_add_overflow, but add type checks similar to
|
|
* below.
|
|
*/
|
|
#define check_add_overflow(a, b, d) ({ \
|
|
typeof(a) __a = (a); \
|
|
typeof(b) __b = (b); \
|
|
typeof(d) __d = (d); \
|
|
(void) (&__a == &__b); \
|
|
(void) (&__a == __d); \
|
|
__builtin_add_overflow(__a, __b, __d); \
|
|
})
|
|
|
|
#define check_sub_overflow(a, b, d) ({ \
|
|
typeof(a) __a = (a); \
|
|
typeof(b) __b = (b); \
|
|
typeof(d) __d = (d); \
|
|
(void) (&__a == &__b); \
|
|
(void) (&__a == __d); \
|
|
__builtin_sub_overflow(__a, __b, __d); \
|
|
})
|
|
|
|
#define check_mul_overflow(a, b, d) ({ \
|
|
typeof(a) __a = (a); \
|
|
typeof(b) __b = (b); \
|
|
typeof(d) __d = (d); \
|
|
(void) (&__a == &__b); \
|
|
(void) (&__a == __d); \
|
|
__builtin_mul_overflow(__a, __b, __d); \
|
|
})
|
|
|
|
#else
|
|
|
|
|
|
/* Checking for unsigned overflow is relatively easy without causing UB. */
|
|
#define __unsigned_add_overflow(a, b, d) ({ \
|
|
typeof(a) __a = (a); \
|
|
typeof(b) __b = (b); \
|
|
typeof(d) __d = (d); \
|
|
(void) (&__a == &__b); \
|
|
(void) (&__a == __d); \
|
|
*__d = __a + __b; \
|
|
*__d < __a; \
|
|
})
|
|
#define __unsigned_sub_overflow(a, b, d) ({ \
|
|
typeof(a) __a = (a); \
|
|
typeof(b) __b = (b); \
|
|
typeof(d) __d = (d); \
|
|
(void) (&__a == &__b); \
|
|
(void) (&__a == __d); \
|
|
*__d = __a - __b; \
|
|
__a < __b; \
|
|
})
|
|
/*
|
|
* If one of a or b is a compile-time constant, this avoids a division.
|
|
*/
|
|
#define __unsigned_mul_overflow(a, b, d) ({ \
|
|
typeof(a) __a = (a); \
|
|
typeof(b) __b = (b); \
|
|
typeof(d) __d = (d); \
|
|
(void) (&__a == &__b); \
|
|
(void) (&__a == __d); \
|
|
*__d = __a * __b; \
|
|
__builtin_constant_p(__b) ? \
|
|
__b > 0 && __a > type_max(typeof(__a)) / __b : \
|
|
__a > 0 && __b > type_max(typeof(__b)) / __a; \
|
|
})
|
|
|
|
/*
|
|
* For signed types, detecting overflow is much harder, especially if
|
|
* we want to avoid UB. But the interface of these macros is such that
|
|
* we must provide a result in *d, and in fact we must produce the
|
|
* result promised by gcc's builtins, which is simply the possibly
|
|
* wrapped-around value. Fortunately, we can just formally do the
|
|
* operations in the widest relevant unsigned type (u64) and then
|
|
* truncate the result - gcc is smart enough to generate the same code
|
|
* with and without the (u64) casts.
|
|
*/
|
|
|
|
/*
|
|
* Adding two signed integers can overflow only if they have the same
|
|
* sign, and overflow has happened iff the result has the opposite
|
|
* sign.
|
|
*/
|
|
#define __signed_add_overflow(a, b, d) ({ \
|
|
typeof(a) __a = (a); \
|
|
typeof(b) __b = (b); \
|
|
typeof(d) __d = (d); \
|
|
(void) (&__a == &__b); \
|
|
(void) (&__a == __d); \
|
|
*__d = (u64)__a + (u64)__b; \
|
|
(((~(__a ^ __b)) & (*__d ^ __a)) \
|
|
& type_min(typeof(__a))) != 0; \
|
|
})
|
|
|
|
/*
|
|
* Subtraction is similar, except that overflow can now happen only
|
|
* when the signs are opposite. In this case, overflow has happened if
|
|
* the result has the opposite sign of a.
|
|
*/
|
|
#define __signed_sub_overflow(a, b, d) ({ \
|
|
typeof(a) __a = (a); \
|
|
typeof(b) __b = (b); \
|
|
typeof(d) __d = (d); \
|
|
(void) (&__a == &__b); \
|
|
(void) (&__a == __d); \
|
|
*__d = (u64)__a - (u64)__b; \
|
|
((((__a ^ __b)) & (*__d ^ __a)) \
|
|
& type_min(typeof(__a))) != 0; \
|
|
})
|
|
|
|
/*
|
|
* Signed multiplication is rather hard. gcc always follows C99, so
|
|
* division is truncated towards 0. This means that we can write the
|
|
* overflow check like this:
|
|
*
|
|
* (a > 0 && (b > MAX/a || b < MIN/a)) ||
|
|
* (a < -1 && (b > MIN/a || b < MAX/a) ||
|
|
* (a == -1 && b == MIN)
|
|
*
|
|
* The redundant casts of -1 are to silence an annoying -Wtype-limits
|
|
* (included in -Wextra) warning: When the type is u8 or u16, the
|
|
* __b_c_e in check_mul_overflow obviously selects
|
|
* __unsigned_mul_overflow, but unfortunately gcc still parses this
|
|
* code and warns about the limited range of __b.
|
|
*/
|
|
|
|
#define __signed_mul_overflow(a, b, d) ({ \
|
|
typeof(a) __a = (a); \
|
|
typeof(b) __b = (b); \
|
|
typeof(d) __d = (d); \
|
|
typeof(a) __tmax = type_max(typeof(a)); \
|
|
typeof(a) __tmin = type_min(typeof(a)); \
|
|
(void) (&__a == &__b); \
|
|
(void) (&__a == __d); \
|
|
*__d = (u64)__a * (u64)__b; \
|
|
(__b > 0 && (__a > __tmax/__b || __a < __tmin/__b)) || \
|
|
(__b < (typeof(__b))-1 && (__a > __tmin/__b || __a < __tmax/__b)) || \
|
|
(__b == (typeof(__b))-1 && __a == __tmin); \
|
|
})
|
|
|
|
|
|
#define check_add_overflow(a, b, d) \
|
|
__builtin_choose_expr(is_signed_type(typeof(a)), \
|
|
__signed_add_overflow(a, b, d), \
|
|
__unsigned_add_overflow(a, b, d))
|
|
|
|
#define check_sub_overflow(a, b, d) \
|
|
__builtin_choose_expr(is_signed_type(typeof(a)), \
|
|
__signed_sub_overflow(a, b, d), \
|
|
__unsigned_sub_overflow(a, b, d))
|
|
|
|
#define check_mul_overflow(a, b, d) \
|
|
__builtin_choose_expr(is_signed_type(typeof(a)), \
|
|
__signed_mul_overflow(a, b, d), \
|
|
__unsigned_mul_overflow(a, b, d))
|
|
|
|
|
|
#endif /* COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW */
|
|
|
|
/**
|
|
* struct_size() - Calculate size of structure with trailing array.
|
|
* @p: Pointer to the structure.
|
|
* @member: Name of the array member.
|
|
* @n: Number of elements in the array.
|
|
*
|
|
* Calculates size of memory needed for structure @p followed by an
|
|
* array of @n @member elements.
|
|
*
|
|
* Return: number of bytes needed or SIZE_MAX on overflow.
|
|
*/
|
|
#define struct_size(p, member, n) \
|
|
__ab_c_size(n, \
|
|
sizeof(*(p)->member) + __must_be_array((p)->member),\
|
|
sizeof(*(p)))
|
|
|
|
/** check_shl_overflow() - Calculate a left-shifted value and check overflow
|
|
*
|
|
* @a: Value to be shifted
|
|
* @s: How many bits left to shift
|
|
* @d: Pointer to where to store the result
|
|
*
|
|
* Computes *@d = (@a << @s)
|
|
*
|
|
* Returns true if '*d' cannot hold the result or when 'a << s' doesn't
|
|
* make sense. Example conditions:
|
|
* - 'a << s' causes bits to be lost when stored in *d.
|
|
* - 's' is garbage (e.g. negative) or so large that the result of
|
|
* 'a << s' is guaranteed to be 0.
|
|
* - 'a' is negative.
|
|
* - 'a << s' sets the sign bit, if any, in '*d'.
|
|
*
|
|
* '*d' will hold the results of the attempted shift, but is not
|
|
* considered "safe for use" if false is returned.
|
|
*/
|
|
#define check_shl_overflow(a, s, d) ({ \
|
|
typeof(a) _a = a; \
|
|
typeof(s) _s = s; \
|
|
typeof(d) _d = d; \
|
|
u64 _a_full = _a; \
|
|
unsigned int _to_shift = \
|
|
_s >= 0 && _s < 8 * sizeof(*d) ? _s : 0; \
|
|
*_d = (_a_full << _to_shift); \
|
|
(_to_shift != _s || *_d < 0 || _a < 0 || \
|
|
(*_d >> _to_shift) != _a); \
|
|
})
|
|
|
|
/**
|
|
* array_size() - Calculate size of 2-dimensional array.
|
|
*
|
|
* @a: dimension one
|
|
* @b: dimension two
|
|
*
|
|
* Calculates size of 2-dimensional array: @a * @b.
|
|
*
|
|
* Returns: number of bytes needed to represent the array or SIZE_MAX on
|
|
* overflow.
|
|
*/
|
|
static inline __must_check size_t array_size(size_t a, size_t b)
|
|
{
|
|
size_t bytes;
|
|
|
|
if (check_mul_overflow(a, b, &bytes))
|
|
return SIZE_MAX;
|
|
|
|
return bytes;
|
|
}
|
|
|
|
/**
|
|
* array3_size() - Calculate size of 3-dimensional array.
|
|
*
|
|
* @a: dimension one
|
|
* @b: dimension two
|
|
* @c: dimension three
|
|
*
|
|
* Calculates size of 3-dimensional array: @a * @b * @c.
|
|
*
|
|
* Returns: number of bytes needed to represent the array or SIZE_MAX on
|
|
* overflow.
|
|
*/
|
|
static inline __must_check size_t array3_size(size_t a, size_t b, size_t c)
|
|
{
|
|
size_t bytes;
|
|
|
|
if (check_mul_overflow(a, b, &bytes))
|
|
return SIZE_MAX;
|
|
if (check_mul_overflow(bytes, c, &bytes))
|
|
return SIZE_MAX;
|
|
|
|
return bytes;
|
|
}
|
|
|
|
static inline __must_check size_t __ab_c_size(size_t n, size_t size, size_t c)
|
|
{
|
|
size_t bytes;
|
|
|
|
if (check_mul_overflow(n, size, &bytes))
|
|
return SIZE_MAX;
|
|
if (check_add_overflow(bytes, c, &bytes))
|
|
return SIZE_MAX;
|
|
|
|
return bytes;
|
|
}
|
|
|
|
#endif /* __LINUX_OVERFLOW_H */
|