zoukankan      html  css  js  c++  java
  • C++编译器优化产生的bug

    long length = OSS_INT64_MAX - 1;

    long offset = OSS_INT64_MAX - 1;

    if (offset < 0) // check 1

    {

      rc = SDB_INVALIDARG;

      goto error;

    }

    if (0 == length || length < -1) // check 2

    {

      rc = SDB_INVALIDARG;

      goto error;

    }

    if (length > 0 && offset + length < 0) // check 3

    {

      rc = SDB_INVALIDARG;

      goto error;

    }

    “check 3”的目的是校验offset + length 的值是否溢出。在debug编译下,“check 3”会执行。但在release模式下,“check 3”不会执行。原因估计是在release模式下,编译器根据“check 1”和“check 2”判断,觉得“check 3”是多余的,于是把这段代码优化删除掉了。

    为了绕过这个优化,可如下修改源码:

    if (length > 0 && (OSS_INT64_MAX  - offset - length) < 0) // check 3

    {

      rc = SDB_INVALIDARG;

      goto error;

    }

  • 相关阅读:
    HDU4465 Candy
    《有关概率和期望问题的研究》读书笔记(完成度:40%)
    HDU5583 Kingdom of Black and White
    Gym100548F Color
    HDU6035 Colorful Tree
    Echarts笔记
    springMVC
    Struts2
    SSH框架学习中遇到的问题
    I/O流
  • 原文地址:https://www.cnblogs.com/withmybag/p/8810538.html
Copyright © 2011-2022 走看看