1. Список говнокодов пользователя j123123

    Всего: 331

  2. Си / Говнокод #23211

    −1

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    Сишечные строки для printf-подобной параши из прошивки ESP8266 (для веб-сервера):
    ....
    method="POST">Login: <INPUT size=10 NAME='login' value="%s"> Password: <INPUT size=10 NAME='pass' value="%s"></td></tr><br><INPUT TYPE='checkbox' NAME='fls'%s> Full Security.
    <input type="hidden" name="st" value=5><br>%s
    <hr><b>Config module:</b><br><div class="spH2"></div><form method="GET">Host name: <INPUT size=12 NAME='hn' value="%s">
    <input type="hidden" name="st" value=7><br>%s
    <hr><b>WiFi options:</b><br><div class="spH2"></div><form method="GET"><input type="radio" name="sm" value="0" %s>Station mode.<input type="radio" name="sm" value="7" %s>AP mode.<br>AP name: <input size=20 name="stname" value="%s"> <br>AP pass: <input size=20 type='password' name="stpass" value="%s"><br>
    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>%s</title><meta http-equiv="REFRESH" content="60"><meta name="viewport" content="width=480" /><meta name="mobile-web-app-capable" content="yes" /><link rel="stylesheet" href="main.css"></head><body><br><div style="text-align: center"><div style="display: inline-block"><div class="name fll">%s<div class="www">MaksMS <a href="http://wifi-iot.com" target="_blank">wifi-iot.com</a><br>

    ... и никакого пхп
    http://wifi-iot.com/ вот тут можно генерировать такие говнопрошивки

    j123123, 23 Июля 2017

    Комментарии (4)
  3. Си / Говнокод #23188

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    char*a??(??)=??<
    "??=include",
    "??=include",
    "??=define o stdout",
    "??=define b break;case",
    "??=define s(p)fputs(p,o);",
    "??=define c(p)fputc(p,o);",
    "void t(p,f)char*p;????/")b'??(':s(??/"???/??/?(??/")b'??)'",
    ":s(??/"???/??/?)??/")b'??/??/??/??/':f&&s(??/"???/??/?/??/")",
    "s(??/"???/??/?/??/")b'??/??/n':if(f)s(??/"???/??/?/n??/")",
    "else case'??/"':if(f)s(??/"???/??/?/??/??/??/"??/")",
    "else default:c(*p)??>??>??>main()??;??/",0);for(p=a",
    ";*p;p++)t(*p,0);exit(!ferror(o)&&",
    "!fclose(o)?EXIT_SUCCESS",
    ":EXIT_FAILURE);",
    "/*NOTREACHED*/",
    "??>",
    0??>;
    ??=include
    ??=include
    ??=define o stdout
    ??=define b break;case
    ??=define s(p)fputs(p,o);
    ??=define c(p)fputc(p,o);
    void t(p,f)char*p;??':s("???/?>")b'??(':s("???/?(")b'??)'
    :s("???/?)")b'??/??/':f&&s("???/?/")
    s("???/?/")b'??/n':if(f)s("???/?/n")
    else case'"':if(f)s("???/?/??/"")
    else default:c(*p)??>??>??>main()??;",0);for(p=a
    ;*p;p++)t(*p,0);exit(!ferror(o)&&
    !fclose(o)?EXIT_SUCCESS
    :EXIT_FAILURE);
    /*NOTREACHED*/
    ??>

    Очередной куайн. Херня на триграфах. Взято отсюда

    http://www.nyx.net/~gthompso/self_c.txt


    Там разумеется есть намного более ебанутые варианты

    j123123, 16 Июля 2017

    Комментарии (0)
  4. Си / Говнокод #23187

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    #include <stdio.h>
    char s[];
    int main ()
    {
        printf ("%s", s);
        for (char *s_p = s; *s_p; s_p++ )
        {
            printf ("%i,", *s_p);
        }
        printf ("};\n");
        return 0;
    }
    
    char s[] = R"(#include <stdio.h>
    char s[];
    int main ()
    {
        printf ("%s", s);
        for (char *s_p = s; *s_p; s_p++ )
        {
            printf ("%i,", *s_p);
        }
        printf ("};\n");
        return 0;
    }
    
    char s[] = {)";

    https://wandbox.org/permlink/uOQ7Uk6uFt4WVS2Y
    код на особом диалекте си (-std=gnu11) который генерирует код на более-менее обычном си (-std=c99) и этот сгенерированный код умеет будет генерировать сам себя
    Основано на сраной параше под названием raw string literals http://govnokod.ru/23178#comment388392 которая оказывается что поддерживается в особом диалекте си (-std=gnu11) https://stackoverflow.com/a/24850262

    j123123, 16 Июля 2017

    Комментарии (9)
  5. Си / Говнокод #23180

    +2

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    #include <stdint.h>
    #include <stdio.h>
    
    char a[] = {35,105,110,99,108,117,100,101,32,60,115,116,100,105,110,116,46,104,62,10,35,105,110,99,108,117,100,101,32,60,115,116,100,105,111,46,104,62,10,10,99,104,97,114,32,97,91,93,32,61,32,123,0,125,59,10,10,105,110,116,32,109,97,105,110,40,118,111,105,100,41,10,123,10,32,32,105,110,116,32,98,59,10,32,32,98,32,61,32,112,114,105,110,116,102,40,34,37,115,34,44,32,97,41,59,10,32,32,102,111,114,32,40,99,104,97,114,32,42,105,32,61,32,97,59,32,105,32,60,32,97,32,43,32,115,105,122,101,111,102,40,97,41,59,32,105,43,43,41,10,32,32,123,10,32,32,32,32,112,114,105,110,116,102,40,34,37,105,44,34,44,32,42,105,41,59,10,32,32,125,10,32,32,112,114,105,110,116,102,40,34,37,115,34,44,32,97,43,98,43,49,41,59,10,32,32,114,101,116,117,114,110,32,48,59,10,125,10,};
    
    int main(void)
    {
      int b;
      b = printf("%s", a);
      for (char *i = a; i < a + sizeof(a); i++)
      {
        printf("%i,", *i);
      }
      printf("%s", a+b+1);
      return 0;
    }

    куайн на сишечке

    j123123, 12 Июля 2017

    Комментарии (4)
  6. C++ / Говнокод #23178

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    #include <algorithm>
    #include <exception>
    #include <iostream>
    #include <memory>
    #include <string>
    #include <vector>
    #include <random>
    #include <chrono>
    #define __CL_ENABLE_EXCEPTIONS
    #include "cl.hpp"
    
    using namespace std::string_literals;
    
    
    int main(int argc, char * argv[]) {
      
      
      size_t data_len = (1024 * 1024 * strtoul(argv[1], nullptr, 10)) / sizeof(uint32_t),
             out_len = strtoul(argv[2], nullptr, 10),
             iter = strtoul(argv[3], nullptr, 10),
             block_size = strtoul(argv[4], nullptr, 10);
      
      std::string src = R"(
        typedef unsigned int uint32_t;
    __kernel void bench(global const uint32_t * data, global uint32_t * out) {
        
        uint32_t res = 0, id = get_global_id(0), next = id;
        for(uint32_t i = 0; i < )"s + std::to_string(iter) + R"(; ++i) {
            for(uint32_t j = 0; j < )" + std::to_string(block_size / sizeof(uint32_t)) +  R"(; ++j)
                res ^= data[next + j];
            next = data[next];
        }
        out[id] = res;
        
    }
      
      )"s;
      
      cl::Program::Sources sources = {{src.data(), src.size()}};
    
    
      std::vector<cl::Platform> platforms;
      cl::Platform::get(&platforms);
      
      std::vector<uint32_t> data(data_len);
      std::vector<uint32_t> out(out_len);
      
      std::generate(std::begin(data), std::end(data), [=,gen = std::mt19937{}]() mutable {
        return gen() % (data_len - (block_size / sizeof(uint32_t)));
      });
      
      for(auto & platform : platforms) {
        std::cout << "Using platform: " << platform.getInfo<CL_PLATFORM_NAME>() << "\n";
        std::vector<cl::Device> devices;
        platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
        for(auto & device : devices) {
          try {
            std::cout << "Using device: " << device.getInfo<CL_DEVICE_NAME>() << "\n";
            cl::Context ctx({device});
            cl::Program program(ctx, sources);
            program.build({device});
            
            cl::Buffer data_buffer(ctx, CL_MEM_READ_WRITE, data.size() * sizeof(uint32_t));
            cl::Buffer out_buffer(ctx, CL_MEM_READ_WRITE, out.size() * sizeof(uint32_t));
            cl::CommandQueue queue(ctx, device);
            queue.enqueueWriteBuffer(data_buffer, CL_TRUE, 0, data.size() * sizeof(uint32_t), data.data());
            
            cl::make_kernel<cl::Buffer &, cl::Buffer &> bench(program, "bench");
            cl::EnqueueArgs eargs(queue,cl::NullRange,cl::NDRange(out.size()),cl::NullRange);
            
            auto start = std::chrono::high_resolution_clock::now();
            bench(eargs, data_buffer, out_buffer).wait();
            auto time = std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::high_resolution_clock::now() - start).count();
            
            size_t ops = out_len * iter;
            size_t total_tp = ops * block_size;
            double miops = (ops / time) / (1000 * 1000);
            double tpgbps = (total_tp / time) / (1024 * 1024 * 1024);
            fprintf(stderr, "Result: %.2fMIOPS, %.2fGB/s, %.2fsec\n", miops, tpgbps, time);
            
            
            queue.enqueueReadBuffer(out_buffer, CL_TRUE, 0, out.size() * sizeof(uint32_t), out.data());   
          
          } catch(cl::Error e) {
            std::cout << e.what() << " : " << e.err() << std::endl;
            std::terminate();
          }
        }
        
      }
    }

    Код Царя
    https://www.linux.org.ru/forum/development/13489159
    https://github.com/superhackkiller1997/gpu_mem_benchmark/blob/master/main.cpp

    j123123, 12 Июля 2017

    Комментарии (59)
  7. C++ / Говнокод #23170

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    int32 documentColorIndex(DocumentData *document, QString &ext) {
    	int32 colorIndex = 0;
    
    	QString name = document ? (document->name.isEmpty() ? (document->sticker() ? lang(lng_in_dlg_sticker) : qsl("Unknown File")) : document->name) : lang(lng_message_empty);
    	name = name.toLower();
    	int32 lastDot = name.lastIndexOf('.');
    	QString mime = document ? document->mime.toLower() : QString();
    	if (name.endsWith(qstr(".doc")) ||
    		name.endsWith(qstr(".txt")) ||
    		name.endsWith(qstr(".psd")) ||
    		mime.startsWith(qstr("text/"))
    		) {
    		colorIndex = 0;
    	} else if (
    		name.endsWith(qstr(".xls")) ||
    		name.endsWith(qstr(".csv"))
    		) {
    		colorIndex = 1;
    	} else if (
    		name.endsWith(qstr(".pdf")) ||
    		name.endsWith(qstr(".ppt")) ||
    		name.endsWith(qstr(".key"))
    		) {
    		colorIndex = 2;
    	} else if (
    		name.endsWith(qstr(".zip")) ||
    		name.endsWith(qstr(".rar")) ||
    		name.endsWith(qstr(".ai")) ||
    		name.endsWith(qstr(".mp3")) ||
    		name.endsWith(qstr(".mov")) ||
    		name.endsWith(qstr(".avi"))
    		) {
    		colorIndex = 3;
    	} else {
    		QChar ch = (lastDot >= 0 && lastDot + 1 < name.size()) ? name.at(lastDot + 1) : (name.isEmpty() ? (mime.isEmpty() ? '0' : mime.at(0)) : name.at(0));
    		colorIndex = (ch.unicode() % 4);
    	}
    
    	ext = document ? ((lastDot < 0 || lastDot + 2 > name.size()) ? name : name.mid(lastDot + 1)) : QString();
    
    	return colorIndex;
    }

    https://github.com/telegramdesktop/tdesktop/blob/5f5770dd46491133b135a71fc2d4f92d13107ade/Telegram/SourceFiles/layout.cpp#L170-L211

    херь из исходниктв Telegram Desktop

    j123123, 10 Июля 2017

    Комментарии (20)
  8. C++ / Говнокод #23161

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    92. 92
    93. 93
    94. 94
    95. 95
    96. 96
    97. 97
    98. 98
    #include <optional>
    
    namespace aim {
    
            template <typename T, template <typename> typename Cont>
            struct is_valid {};
    
            template <typename T, template <typename> typename Cont>
            struct chain;
    
            template <typename T, template <typename> typename Cont>
            struct chain {
                    using value_type_t = T;
                    using holder_t = Cont<value_type_t>;
    
                    chain()
                    {}
    
                    chain(holder_t&& value_)
                    : value(std::move(value_))
                    {}
    
                    template <typename Func>
                    constexpr auto otherwise(Func&& func) {
                            return chain<T, Cont>{std::move(func())};
                    }
    
                    template <typename Func>
                    constexpr auto with(Func&& func) -> chain<value_type_t, Cont> {
                            if (bool(value)) {
                                    return chain<value_type_t, Cont>{Cont<value_type_t>{func(*value)}};
                            }
                            else {
                                    return chain<value_type_t, Cont>{};
                            }
                    }
    
                    holder_t value;
            };
    
            template <typename T, template <typename> typename Cont>
            chain(Cont<T>&&) -> chain<T, Cont>;
    
            template <typename T, template <typename> typename Cont>
            struct use {
                    using value_type_t = T;
                    using holder_t = Cont<value_type_t>;
    
                    use(holder_t&& value)
                    : value(std::move(value))
                    {}
    
                    template <typename Func>
                    constexpr auto with(Func&& func) -> chain<value_type_t, Cont> {
                            if (is_valid<T, Cont>{}(value)) {
                                    return chain<value_type_t, Cont>{Cont<value_type_t>{func(*value)}};
                            }
                            else {
                                    return chain<value_type_t, Cont>{};
                            }
                    }
    
                    holder_t value;
            };
    
            template <typename T, template <typename> typename Cont>
            use(Cont<T>&&) -> use<T, Cont>;
            
    }
    
    namespace aim {
            template <typename T>
            struct is_valid<T, std::optional> {
                    constexpr bool operator()(std::optional<T> const& value) {
                            return bool(value);
                    }
            };
    }
    
    #include <iostream>
    #include <memory>
    
    int main() {
            {
                    std::optional<int> a;
    
                    aim::use(std::move(a)).with([](auto v) {
                            std::cout << v << '\n';
                            return 1;
                    }).otherwise([](){
                            std::cout << "it's none!\n";
                            return 2;
                    }).with([](auto v){
                            std::cout <<  v << '\n';
                            return 3;
                    });
            }
    }

    говно

    j123123, 06 Июля 2017

    Комментарии (7)
  9. Си / Говнокод #23156

    −1

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    static bool
    do_convert(iconv_t cd, const char * src, int src_len,
               char ** dst, int *dst_len)
      {
      char * ret;
    
      char *inbuf;
      char *outbuf;
      int alloc_size;
      int output_pos;
      size_t inbytesleft;
      size_t outbytesleft;
    
      if(src_len < 0)
        src_len = strlen(src);
    #if 0
      fprintf(stderr, "Converting:\n");
      bgav_hexdump(src, src_len, 16);
    #endif
      alloc_size = src_len + BYTES_INCREMENT;
    
      inbytesleft  = src_len;
    
      /* We reserve space here to add a final '\0' */
      outbytesleft = alloc_size-1;
    
      ret    = malloc(alloc_size);
    
      inbuf  = (char *)src;
      outbuf = ret;
    
      while(1)
        {
    
        if(iconv(cd, (ICONV_CONST char **)&inbuf, &inbytesleft,
                 &outbuf, &outbytesleft) == (size_t)-1)
          {
          switch(errno)
            {
            case E2BIG:
              output_pos = (int)(outbuf - ret);
    
              alloc_size   += BYTES_INCREMENT;
              outbytesleft += BYTES_INCREMENT;
    
              ret = realloc(ret, alloc_size);
              if (ret == NULL)
                {
                cdio_warn("Can't realloc(%d).", alloc_size);
                return false;
                }
              outbuf = ret + output_pos;
              break;
            default:
              cdio_warn("Iconv failed: %s", strerror(errno));
              if (ret != NULL)
                free(ret);
              return false;
              break;
            }
          }
        if(!inbytesleft)
          break;
        }
      /* Zero terminate */
      *outbuf = '\0';
    
      /* Set return values */
      *dst = ret;
      if(dst_len)
        *dst_len = (int)(outbuf - ret);
    #if 0
      fprintf(stderr, "Conversion done, src:\n");
      bgav_hexdump(src, src_len, 16);
      fprintf(stderr, "dst:\n");
      bgav_hexdump((uint8_t*)(ret), (int)(outbuf - ret), 16);
    #endif
      return true;
    }

    https://github.com/pbatard/rufus/blob/edcfd43ed550b17f23f9534f4b3b5cdd7fffdefc/src/libcdio/driver/utf8.c#L170-L248

    А вы тут видите тут баг?

    j123123, 05 Июля 2017

    Комментарии (16)
  10. Си / Говнокод #23155

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    void SetTitleBarIcon(HWND hDlg)
    {
    	int i16, s16, s32;
    	HICON hSmallIcon, hBigIcon;
    
    	// High DPI scaling
    	i16 = GetSystemMetrics(SM_CXSMICON);
    	// Adjust icon size lookup
    	s16 = i16;
    	s32 = (int)(32.0f*fScale);
    	if (s16 >= 54)
    		s16 = 64;
    	else if (s16 >= 40)
    		s16 = 48;
    	else if (s16 >= 28)
    		s16 = 32;
    	else if (s16 >= 20)
    		s16 = 24;
    	if (s32 >= 54)
    		s32 = 64;
    	else if (s32 >= 40)
    		s32 = 48;
    	else if (s32 >= 28)
    		s32 = 32;
    	else if (s32 >= 20)
    		s32 = 24;
    
    	// Create the title bar icon
    	hSmallIcon = (HICON)LoadImage(hMainInstance, MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, s16, s16, 0);
    	SendMessage (hDlg, WM_SETICON, ICON_SMALL, (LPARAM)hSmallIcon);
    	hBigIcon = (HICON)LoadImage(hMainInstance, MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, s32, s32, 0);
    	SendMessage (hDlg, WM_SETICON, ICON_BIG, (LPARAM)hBigIcon);
    }

    https://github.com/pbatard/rufus/blob/92d046e663811fd922262f1f347ad2ebe8e09b97/src/stdlg.c#L1607-L1639

    j123123, 05 Июля 2017

    Комментарии (4)
  11. Си / Говнокод #23143

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    #include <stdio.h>;
    
    void check(int x, int y) {
      if (2*x == y && y < 0 && 0 <= 2*x) {
        puts("Impossible!");
      }
    }
    
    int main() {
      check(0x7F80007F, 0xFF0000FE);
    }

    https://runtimeverification.com/blog/?p=257
    When writing code for a specific compiler you can rely on the implementation-specified behavior, but signed overflow is still problematic. GCC promises that conversions between integer types will be reduced modulo the appropriate power of two when the value is not representable in the target type. This means that with GCC the conversion above will initialize var to -0x112234 on any architecture that GCC supports. However, only initialization and other conversions are safe. GCC still considers signed overflow in arithmetic as undefined behavior, and optimizes under the assumption that there will be no overflow. This can lead to apparently impossible results when signed values do overflow. Compiled with -O3, this program prints “Impossible!”.

    By adding apparently-redundant casts to 2*x to give (int)(2*(unsigned int)x), the calculation becomes implementation-specified behavior from an out-of-range conversion instead of undefined behavior. While this code may not be portable between compilers, GCC now guarantees the “impossible” code will not be executed even with -O3.

    j123123, 20 Июня 2017

    Комментарии (2)