1. Лучший говнокод

    В номинации:
    За время:
  2. Си / Говнокод #27559

    +3

    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
    #include <stdlib.h>
    #include <stdio.h>
    #include <inttypes.h>
    
    #define STRING 0
    #define INTEGER 1
    
    
    #define CAT(x,y) x ## _ ## y
    #define J(x,y)  CAT(x,y)
    
    
    typedef union
    {
      char *J(v, STRING);
      int J(v,INTEGER);
    } Un;
    
    typedef struct
    {
      uint8_t Obj_t;
      Un u;
    } Object;
    
    #define IF_INSTOF(var, t, newvar) \
    if(var.Obj_t == t) \
    { \
      typeof(var.u.J(v,t)) *newvar = &var.u.J(v,t);
    
    int main(void)
    {
      Object obj1 = {STRING, {.J(v,STRING) = "1"}};
      
      IF_INSTOF(obj1,STRING,str)
        printf("String: %s\n", *str);
      }
      else
      {
        printf("Not a string\n");
      }
    
      Object obj2 = {INTEGER, {.J(v,INTEGER) = 1}};
      IF_INSTOF(obj2,INTEGER,i)
        printf("Integer: %d\n", *i);
      }
      else
      {
        printf("Not an Integer\n");
      }
    
      return EXIT_SUCCESS;
    }

    Вот такие смарткасты через препроцессор.

    https://govnokod.ru/27556#comment655527

    j123123, 13 Августа 2021

    Комментарии (32)
  3. PHP / Говнокод #27531

    +3

    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
    do {           
                $entries = $xpath->query("//div[@class='identity']/img");
                if(isset($entries[0])) break;
                $entries = $xpath->query("//h1[@class='avatared']/a/img");
                if(isset($entries[0])) break;
                $entries = $xpath->query("//div[@class='avatared']/a/img");
                if(isset($entries[0])) break;
                $entries = $xpath->query("//div[@itemtype='http://schema.org/Person']/a/img");
            } while(false);
            if(!isset($entries[0])) continue;
    
            $src = $entries[0]->getAttribute('src');
            if(!preg_match('#[/=]([0-9a-f]{32})[\?&]#', $src, $matches)) continue;
            $hash = $matches[1];
    
    // спустя несколько строк
    
            do {           
                $entries = $xpath->query("//div[@class='email']/script");
                if(isset($entries[0])) break;
                $entries = $xpath->query("//dl/dd[@class='email']/script");
            } while(false);
            if(isset($entries[0])) {
                $rawcode = $entries[0]->textContent;
                if(!preg_match("#eval\(decodeURIComponent\('(.*)'\)\)#", $rawcode, $matches)) continue;
                $rawcode2 = urldecode($matches[1]);
                if(!preg_match('#href=\\\\?"mailto:([^"\\\\]*)\\\\?"#', $rawcode2, $matches)) continue;
                $email = $matches[1];
                unset($entries);
            } else do {
                $entries = $xpath->query("//div[@class='avatared']/div[@class='details']/dl/dd/a[@data-email]");
                if(isset($entries[0])) break;
                $entries = $xpath->query("//ul[@class='vcard-details']/li[@class='vcard-detail']/a[@data-email]");
            } while(false);
            if(isset($entries[0])) {
                $email = urldecode($entries[0]->getAttribute('data-email'));
            }

    Прототип программы, вытягивающей хэш аватарки и е-мейл из архивной копии профиля в «Гитхабе».

    Nyancat, 21 Июля 2021

    Комментарии (5)
  4. PHP / Говнокод #27506

    +3

    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
    $a != abs($a)
    $a+abs($a) == 0
    $a && !($a + abs($a))
    $n>>1 > $n
    substr_count($a,"-")
    is_nan(sqrt($number))
    is_nan(log($n))
    !array_shift(explode("-", $num))
    (int)$var === ~~(int)$var
    strlen(strval($num)) != strlen(strval(abs($num)))
    strlen(decbin($n)) == 32
    is_int(strpos(get_headers("http://habrahabr.ru/blogs/php/page$num/")[0], '404'));
    
    function lessThanZero ($num) {
        while (1) {
            if ($num++ == 0) {
                return true;
            }
        }
    }
    
    function is_value_between($value, $begin, $end) {
        return in_array($value, range($begin,$end));
    }

    "Как проверять отрицательное ли число ?
    В мануале в математических функциях не нашёл ."

    https://php.ru/forum/threads/kak-proverjat-otricatelnoe-li-chislo.8208/

    kezzyhko, 09 Июля 2021

    Комментарии (74)
  5. Куча / Говнокод #27504

    +3

    1. 1
    IT Оффтоп #99

    #69: https://govnokod.ru/27071 https://govnokod.xyz/_27071
    #70: https://govnokod.ru/27097 https://govnokod.xyz/_27097
    #71: https://govnokod.ru/27115 https://govnokod.xyz/_27115
    #72: https://govnokod.ru/27120 https://govnokod.xyz/_27120
    #73: https://govnokod.ru/27136 https://govnokod.xyz/_27136
    #74: https://govnokod.ru/27160 https://govnokod.xyz/_27160
    #75: https://govnokod.ru/27166 https://govnokod.xyz/_27166
    #76: https://govnokod.ru/27168 https://govnokod.xyz/_27168
    #77: https://govnokod.ru/27186 https://govnokod.xyz/_27186
    #78: https://govnokod.ru/27219 https://govnokod.xyz/_27219
    #79: https://govnokod.ru/27254 https://govnokod.xyz/_27254
    #80: https://govnokod.ru/27270 https://govnokod.xyz/_27270
    #81: https://govnokod.ru/27280 https://govnokod.xyz/_27280
    #82: https://govnokod.ru/27284 https://govnokod.xyz/_27284
    #83: https://govnokod.ru/27296 https://govnokod.xyz/_27296
    #84: https://govnokod.ru/27336 https://govnokod.xyz/_27336
    #85: https://govnokod.ru/27381 https://govnokod.xyz/_27381
    #86: https://govnokod.ru/27405 https://govnokod.xyz/_27405
    #87: https://govnokod.ru/27429 https://govnokod.xyz/_27429
    #88: https://govnokod.ru/27432 https://govnokod.xyz/_27432
    #89: https://govnokod.ru/27435 https://govnokod.xyz/_27435
    #90: https://govnokod.ru/27439 https://govnokod.xyz/_27439
    #91: https://govnokod.ru/27449 https://govnokod.xyz/_27449
    #92: https://govnokod.ru/27460 https://govnokod.xyz/_27460
    #93: https://govnokod.ru/27463 https://govnokod.xyz/_27463
    #94: https://govnokod.ru/27466 https://govnokod.xyz/_27466
    #95: https://govnokod.ru/27473 https://govnokod.xyz/_27473
    #96: https://govnokod.ru/27478 https://govnokod.xyz/_27478
    #97: https://govnokod.ru/27484 https://govnokod.xyz/_27484
    #98: https://govnokod.ru/27495 https://govnokod.xyz/_27495

    nepeKamHblu_nemyx, 07 Июля 2021

    Комментарии (2447)
  6. Си / Говнокод #27477

    +3

    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
    #include <stdio.h>
    #include <stdlib.h>
    
    #define DEREF(x) ({*(x);})
    #define PTR(x) ({&(x);})
    #define PTR_T(x) typeof(typeof(x) *)
    #define DEREF_T(x) typeof( ({typeof(x) DEREF_T; *DEREF_T;}) )
    
    
    void add5(PTR_T(int) a)
    {
      DEREF(a)+= 5;
    }
    
    int main(void)
    {
      int a = 0;
      printf("%d\n", a);
      add5(PTR(a));
      printf("%d\n", a);
      
      int b = 5;
      PTR_T(b) b_p = PTR(b); // int *
      printf("%d\n", DEREF(b_p) );
      
      DEREF_T(b_p) c = 666; // int
      printf("%d", c);
      
      return EXIT_SUCCESS;
    }

    https://govnokod.ru/27476#comment637183

    ASD_77:
    потому что я люблю Си и С++ но ненажижу * и & ... поэтому я решил исправить этот недочет в мире

    Исправил, проверь. Компилится только в GCC, всё-таки Clang не 100% совместим c гнутыми расширениями.

    j123123, 21 Июня 2021

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

    +3

    1. 1
    2. 2
    3. 3
    //! Set this value as an empty object.
    /*! \post IsObject() == true */
    GenericValue& SetObject() { this->~GenericValue(); new (this) GenericValue(kObjectType); return *this; }

    Парсер json'ок писали сверхлюди

    https://github.com/Tencent/rapidjson/blob/master/include/rapidjson/document.h#L1188

    mvngr, 16 Июня 2021

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

    +3

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    std::cout << "Creating ptr1!" << std::endl;
    auto ptr1 = make_nft<Cow>();
    std::cout << "ptr1(" << &ptr1 << "): " << ptr1.get() << std::endl;
    ptr1->MakeSound();
    
    std::cout << "Creating ptr2!" << std::endl;
    nft_ptr<Animal> ptr2;
    std::cout << "ptr2(" << &ptr2 << "): " << ptr2.get() << std::endl;
    std::cout << "Moving: ptr2 = std::move(ptr1)" << std::endl;
    ptr2 = std::move(ptr1);
    std::cout << "Moved: ptr1 = " << ptr1.get() << " ptr2 = " << ptr2.get()
              << std::endl;

    https://github.com/zhuowei/nft_ptr

    "C++ std::unique_ptr that represents each object as an NFT on the Ethereum blockchain."

    PolinaAksenova, 13 Апреля 2021

    Комментарии (23)
  9. Python / Говнокод #27351

    +3

    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
    """
    A module for printing funny frames.
    Copyright (C) 2021 Ingostnus.
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
    
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
    """
    
    from math import sqrt
    
    _str = "sample"                  # String to be turned into a funny frame.
    _sep = ' '                       # Separator, i.e. a string to be inserted between each letter of the _str.
    _vertical_separator_mode = 'F'   # Determines whether separator should be printed between each row.
                                     # ... 'F' --> off. 'T' --> on.
    
    def get_user_input() -> None:
        """
        This function lets user input desired values and override the defaults.
        """
        global _str; _str = str(input("String: "))
        global _sep; _sep = str(input("Character: "))
        global _vertical_separator_mode; _vertical_separator_mode = str((input("VSM (T/F):")))
        if _vertical_separator_mode == 'T':
            print("VMS is ON!!!")
    
    def print_frame(printer = print) -> list:
        """
        This function is designed for printing a frame. Custom printer function
        may be supplied to process the output in a specific way.
        """
    
        buffer_len = (len(_str) + len(_sep) * (len(_str) - 1))**2
    
        # First line.
        printer(''.join([_str[i] + _sep if i < (len(_str) - 1) else _str[-1] for i in range(len(_str))]))
        
        # Second -- pre last lines.
        empty_space = ' ' * (int(sqrt(buffer_len)) - 2)
        for i in range(1, len(_str) - 1):
            # If vertical separator mode is toggled, print vertical separator.
            if _vertical_separator_mode == 'T':
                printer(_sep + empty_space + _sep)
            printer(_str[i] + empty_space + _str[-(i + 1)])
        
        # Last line.
        printer(''.join([_str[-(i + 1)] + _sep if i < (len(_str) - 1) else _str[0] for i in range(len(_str))]))
    
    # To give the best perfomance and flexibility, this module should be used as
    # an imported library. Though, its basic functionality can be used even if
    # it's executed directly.
    if __name__ == '__main__':
        get_user_input()
        print_frame()

    Переписала код https://govnokod.ru/27348 на питон, добавив чуть-чуть улучшений и немноже4ко документацци.

    KoWe4Ka_l7porpaMMep, 11 Апреля 2021

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

    +3

    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
    // https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    
    
    int main(void)
    {
      char *a = "petuh";
      if(({bool ret = 0;if(a[0]=='p')if(a[1]=='e')if(a[2]=='t')if(a[3]=='u')if(a[4]=='h')ret=1;ret;}))
      {
        puts(a);
      }
      return EXIT_SUCCESS;
    }

    Интересное расширение.

    j123123, 09 Апреля 2021

    Комментарии (33)
  11. C++ / Говнокод #27311

    +3

    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
    // A registrator class.
    // To register a Parents <- Child relation user must derive their type from
    // the correct specialization of this class
    template<typename Child, typename... Parents>
    struct registrator {
    private:
        // The registerRelation() function would be called during dynamic initialization of this
        // static storage duration variable.
        static volatile inline detail::registratorType _registrator
            = RelationManager::registerRelation<Child, Parents...>();
    
    protected:
        /*
        [basic.start.dynamic]/6:
            It is implementation-defined whether the dynamic initialization of a non-block inline variable
            with static storage duration is sequenced before the first statement of main or is deferred.
            If it is deferred, it strongly happens before any non-initialization odr-use of that variable.
        [basic.start.dynamic]/4:
            A non-initialization odr-use is an odr-use ([basic.def.odr]) not caused directly or indirectly
            by the initialization of a non-block static or thread storage duration variable.
        By odr-using the _registrator here we are making sure that any Child constructor is odr-using it as well,
        thus guaranteeing that the compiler would call registerRelation() strongly before the Child constructor
        */
        constexpr registrator() noexcept
        {
            // Taking an address of a variable, even in a discarded statement, is an odr-use
            (void)&_registrator;
        }
    
    private:
        /*
        If registrator() constructor is never 'non-initialization odr-used'
        (no Child objects are created besides non-local static ones), then the only
        instantiation of the registrator class is an implicit instantiation caused by deriving Child;
    
        [temp.inst]/3:
            The implicit instantiation of a class template specialization causes
            (3.1) -- the implicit instantiation of the declarations, but not of the definitions, of
                the non-deleted class member functions, member classes, scoped member enumerations,
                static data members, member templates, and friends; and
            (3.2) -- the implicit instantiation of the definitions of deleted member functions,
                unscoped member enumerations, and member anonymous unions.
    
        [basic.def.odr]/8:
            [...] A constructor for a class is odr-used as specified in [dcl.init].
        (In other words, "A constructor (including default constructors) for a class
         is odr-used by the initialization that selects it.")
    
        The problem is that if there is no Child() constructor calls, then the compiler is not required to
        even DEFINE our registrator() constructor, and since there is only one odr-use of the static inline
        variable _registrator, the compiler is not required to generate a definition for it as well. And since
        it is not generating a definition, the initialization is also not generated.
    
        But [temp.inst]/3.2 requires that any implicit instantiation of a class template also
        causes the DEFINITION instantiation of an unscoped member enumerations. Next,
        [dcl.spec.auto.general]/12:
            Return type deduction for a templated entity that is a function or function template
            with a placeholder in its declared type occurs when the definition is instantiated
            even if the function body contains a return statement with a non-type-dependent operand
        Thus, using sizeof(*_force_registrator_instantiation()) causes the implicit instantiation
        of definition of _force_registrator_instantiation(), and since this function is odr-using _registrator,
        its definition (and initialization) is also generated.
    
        The only problem is that by [basic.start.dynamic]/6 the implementation MAY defer the dynamic
        initialization of _registrator to the point where it is 'non-initialization odr-used', and since without
        the Child() constructor call we don't have any way to non-initialization odr-use it, we also have no way to
        make sure that the compiler invokes registerRelation() before main() or before any other point of execution.
        The good news, though, is that all three modern compilers obediently initializes _registrator before the first
        statement of main().
        */
        static auto _force_registrator_instantiation()
        {
            return &_registrator;
        }
    
        enum {
            _ROTARTSIGER_ETAITNATSNI = sizeof(*_force_registrator_instantiation()),
        };
    };

    PolinaAksenova, 23 Марта 2021

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