Skip to content

[BUG] overflow/underflow are not detected for long option input #801

@KuhakuPixel

Description

@KuhakuPixel

Problem:
I have the following code

/*
 * example of inputting any numeric type with CLI11
 * */
#include "CLI11.hpp"
#include <stdint.h>
#include <stdio.h>

template <typename T> class arg_vals {
public:
  T num;
};

template <typename T> T run_command(int argc, char **argv) {
  arg_vals<T> args;
  CLI::App app{"App description"};

  app.add_option("-p", args.num, "Parameter")->required();

  CLI11_PARSE(app, argc, argv);

  printf("%s\n", std::to_string(args.num).c_str());
  return 0;
}
int main(int argc, char **argv) {
  run_command<short>(argc, argv);
  return 0;
}

Compiling with

g++ main.cpp -g -o main

and running with

# overflow for short
./main -p 11111111111111111111

CLI11 detects correctly that the number is too big for a short
by outputting

Could not convert: -p = 11111111111111111111
Run with --help for more information.

It also work with

run_command<int>(argc, argv);

but not with

run_command<long>(argc, argv);

running with

./main -p 11111111111111111111111

only gives LONG_MAX and doesn't detect
overflow/underflow correctly

9223372036854775807

Expected output for

run_command<long>(argc, argv);

are

Could not convert: -p = 11111111111111111111
Run with --help for more information.

System Info:

CLI version: 2.3.1
64 bit

g++ -v

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.3.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-xKiWfi/gcc-11-11.3.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-xKiWfi/gcc-11-11.3.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.3.0 (Ubuntu 11.3.0-1ubuntu1~22.04) 
uname -a
Linux kuhakupixel-X 5.17.1-051701-generic #202203280950 SMP PREEMPT Mon Mar 28 09:59:31 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions