警告:标志“ 0”会导致带有“ s”转换说明符的未定义行为

时间:2019-11-12 20:09:38

标签: c++

在Visual Studio 2017中编译应用程序时-输出日志显示错误,内容如下:

warning: flag '0' results in undefined behavior with 's' conversion specifier [-Wformat]
                printf("[ERROR] START/END IsGreater %064s \n", maxKey.GetBase16().c_str());
                                                    ~^~~~
warning: flag '0' results in undefined behavior with 's' conversion specifier [-Wformat]
               printf("[load] start=%064s \n", bc->ksStart.GetBase16().c_str());
                                    ~^~~~
warning: flag '0' results in undefined behavior with 's' conversion specifier [-Wformat]
               printf("[load]  next=%064s \n", bc->ksNext.GetBase16().c_str());
                                    ~^~~~
warning: flag '0' results in undefined behavior with 's' conversion specifier [-Wformat]
               printf("[load]   end=%064s \n", bc->ksFinish.GetBase16().c_str());
                                    ~^~~~
warning: flag '0' results in undefined behavior with 's' conversion specifier [-Wformat]
              fprintf(f, "Priv (HEX): 0x%064s\n", pAddrHex.c_str());
                                        ~^~~~
warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
            lambda.SetBase16("5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72");

消除现有问题看起来应该如何正确?

以下是从相关文件的源代码中截取的部分:

void checkKeySpace(BITCRACK_PARAM * bc, Int& maxKey) {

    if (bc->ksStart.IsGreater(&maxKey) || bc->ksFinish.IsGreater(&maxKey)) {
        printf("[ERROR] START/END IsGreater %064s \n", maxKey.GetBase16().c_str());
        exit(-1);
    }
    if (bc->ksFinish.IsLowerOrEqual(&bc->ksStart)) {
        printf("[ERROR] END IsLowerOrEqual START \n");
        exit(-1);
    }
    if (bc->ksFinish.IsLowerOrEqual(&bc->ksNext)) {
        printf("[ERROR] END: IsLowerOrEqual NEXT \n");
        exit(-1);
    }

    return;
}
void reconstructAdd(Secp256K1 *secp, string fileName, string outputFile, string privAddr) {

  bool compressed;
  int addrType;
  Int lambda;
  Int lambda2;
  lambda.SetBase16("5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72");
  lambda2.SetBase16("ac9c52b33fa3cf1f5ad9e3fd77ed9ba4a880b9fc8ec739c2e0cfc810b51283ce");

  Int privKey = secp->DecodePrivateKey((char *)privAddr.c_str(),&compressed);
  if(privKey.IsNegative())
    exit(-1);

1 个答案:

答案 0 :(得分:2)

对于%s格式说明符,标准中未定义零填充。仅对以下转换有效:%d,%i,%o,%u,%x,%X,%a,%A,%e,%E,%f,%F,%g和%G。

这是cppreference的引文:

  

0:用于整数和浮点数转换,前导零   用于填充字段而不是空格字符。对于整数   如果明确指定精度,则将其忽略。对于   使用此标志的其他转换将导致未定义的行为。它是   如果存在-标志,则忽略。

这就是您的编译器警告的内容。