univalue: avoid warning: array subscript is of type 'char' #4714

pull isghe wants to merge 3 commits into bitcoin:master from isghe:univalue_avoid_wchar_subscripts changing 1 files +8 −8
  1. isghe commented at 3:15 PM on August 17, 2014: contributor

    To avoid on: g++ --version Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) Target: x86_64-apple-darwin13.3.0 Thread model: posix

    the following warnings: CXX univalue/univalue_write.o univalue/univalue_write.cpp:33:12: warning: array subscript is of type 'char' [-Wchar-subscripts] escapes['"'] = "\""; ^~~~ univalue/univalue_write.cpp:34:12: warning: array subscript is of type 'char' [-Wchar-subscripts] escapes[''] = "\"; ^~~~~ univalue/univalue_write.cpp:35:12: warning: array subscript is of type 'char' [-Wchar-subscripts] escapes['/'] = "/"; ^~~~ univalue/univalue_write.cpp:36:12: warning: array subscript is of type 'char' [-Wchar-subscripts] escapes['\b'] = "\b"; ^~~~~ univalue/univalue_write.cpp:37:12: warning: array subscript is of type 'char' [-Wchar-subscripts] escapes['\f'] = "\f"; ^~~~~ univalue/univalue_write.cpp:38:12: warning: array subscript is of type 'char' [-Wchar-subscripts] escapes['\n'] = "\n"; ^~~~~ univalue/univalue_write.cpp:39:12: warning: array subscript is of type 'char' [-Wchar-subscripts] escapes['\r'] = "\r"; ^~~~~ univalue/univalue_write.cpp:40:12: warning: array subscript is of type 'char' [-Wchar-subscripts] escapes['\t'] = "\t"; ^~~~~ 8 warnings generated.

  2. univalue: avoid warning: array subscript is of type 'char' 8cfbd17d51
  3. in src/univalue/univalue_write.cpp:None in 8cfbd17d51 outdated
      21 | -    escapes['\b'] = "\\b";
      22 | -    escapes['\f'] = "\\f";
      23 | -    escapes['\n'] = "\\n";
      24 | -    escapes['\r'] = "\\r";
      25 | -    escapes['\t'] = "\\t";
      26 | +    escapes[static_cast <unsigned char> ('"')] = "\\\"";
    


    sipa commented at 3:35 PM on August 17, 2014:

    Can you avoid the spaces before and after < and >?

  4. jgarzik commented at 3:43 PM on August 17, 2014: contributor

    Ugh. The compiler is being very dumb here, as the value range is self-evidently known at compile time.

    I wonder if a 256-element initializer would do the trick with less ugliness...

  5. univalue: avoid warning: array subscript is of type 'char'
    removed extra spaces before and after <>
    c356616f41
  6. Merge branch 'univalue_avoid_wchar_subscripts' of https://github.com/isghe/bitcoin into univalue_avoid_wchar_subscripts
    Conflicts:
    	src/univalue/univalue_write.cpp
    0362aaf525
  7. jgarzik referenced this in commit f30b421531 on Aug 17, 2014
  8. jgarzik commented at 4:20 PM on August 17, 2014: contributor

    A better solution is to avoid initializing this array at runtime at every program invocation.

  9. laanwj commented at 7:11 AM on August 18, 2014: member

    Right, I understand the reason of this warning. Using chars for array indexes is dangerous as they can be negative. However in this case the exact value is known to the compiler and larger than 0 so it's quite silly...

  10. laanwj commented at 7:19 AM on August 18, 2014: member

    Closing in favor of #4715

  11. laanwj closed this on Aug 18, 2014

  12. jgarzik referenced this in commit 3cceba7abb on Aug 18, 2014
  13. reddink referenced this in commit cc589f2ccd on May 27, 2020
  14. MarcoFalke locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-20 00:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me