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

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

    +135

    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
    int log2_floor (unsigned x)
    {
        #define NIMHTNOE0WNM(n) (((~(x>>n)+1)>>n)&n)
     
        int res, n;
     
        n = NIMHTNOE0WNM(16); res  = n; x >>= n;
        n = NIMHTNOE0WNM( 8); res |= n; x >>= n;
        n = NIMHTNOE0WNM( 4); res |= n; x >>= n;
        n = NIMHTNOE0WNM( 2); res |= n; x >>= n;
        n = NIMHTNOE0WNM( 1); res |= n;
        return res;
    }

    Кто-то Воррена перечитал.

    codemonkey, 11 Декабря 2014

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

    +135

    1. 1
    return (true == is_break) ? resource : NULL;

    Вонъ изъ профессiи!

    codemonkey, 07 Декабря 2014

    Комментарии (12)
  4. Куча / Говнокод #17247

    +135

    1. 1
    <html class=" js flexbox flexboxlegacy hashchange history rgba hsla multiplebgs backgroundsize borderimage borderradius boxshadow textshadow opacity flash no-touch no-opera cssanimations csscolumns cssgradients no-cssreflections csstransforms csstransforms3d csstransitions fontface generatedcontent video audio localstorage applicationcache performance mediaqueries" lang="ru-RU" xml:lang="ru-RU" style="">

    kyzi007, 03 Декабря 2014

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

    +135

    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
    public CookieContainer GetCookies(string url, string login, string password)
    {
        try
        {
            var cookies = new CookieContainer();
            string postData = string.Format(@"subaction=dologin&username={0}&password={1}&selected_language=Russian&x=62&y=37", Uri.EscapeDataString(login), Uri.EscapeDataString(password));
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url + "admin.php");
            httpWebRequest.AllowAutoRedirect = true;
            httpWebRequest.CookieContainer = cookies;
            httpWebRequest.Method = "POST";
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.UserAgent = "Opera/9.80 (Windows NT 6.1; U; ru) Presto/2.10.289 Version/12.01";
            httpWebRequest.ServicePoint.Expect100Continue = false;
            byte[] ByteQuery = System.Text.Encoding.UTF8.GetBytes(postData);
            httpWebRequest.ContentLength = ByteQuery.Length;
            Stream QueryStream = httpWebRequest.GetRequestStream();
            QueryStream.Write(ByteQuery, 0, ByteQuery.Length);
            QueryStream.Close();
            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.GetEncoding(1251));
            string loginPage = sr.ReadToEnd();
            sr.Close();
            if (loginPage.IndexOf(@"div class=""error""") == -1)
            {
                httpWebResponse.Cookies = httpWebRequest.CookieContainer.GetCookies(httpWebRequest.RequestUri);
                httpWebResponse.Close();
                return cookies;
            }
            else
            {
                return null;
            }
            
        }
        catch (Exception)
        {
            if (n < 3)
            {
                Thread.Sleep(400);
                n++;
                return GetCookies(url, login, password);
            }
            else
            {
                n = 0;
                return null;
            }
        }
    }

    Костыльно-ориентированное велосипедирование. Выдержка из паттерна "тулза для работы с вебом", метод авторизации на какой-то из CMS.

    pushistayapodmyshka, 27 Ноября 2014

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

    +135

    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
    public void Login(ClientHandlers<int> actions)
            {
                var request = GetGetRequest("/user/xml/{0}", _login);
    
                var act = new Action<IRestResponse<GetUserInfoResult>>(response =>
                    {
                        if (CheckUserInfo(response))
                        {
                            throw new Exception("Невозможно получить информацию о пользователе\r\nНеобработанное исключение");
                        }
    
                        UserInfo = response.Data.UserInfo;
                        actions.Completed(UserInfo.idUser);
                    });
                _client.ExecuteAsync(request, act);
            }
    
            private bool CheckUserInfo(IRestResponse<GetUserInfoResult> response)
            {
                if (response != null && response.Data != null && response.Data.UserInfo != null && response.Data.UserInfo.idUser != 0)
                {
                    return true;
                }
    
                var message = "Невозможно получить информацию о пользователе";
                if (response == null)
                {
                    message = string.Format("{0}\r\n{1}", 
                                            message, 
                                            "Response is null");
                    throw new Exception(message);
                }
    
                message = string.Format("{0}\r\nResponse data is null\r\nContext: {1}", 
                                        message, 
                                        response.Content);
    
                if (response.Data == null)
                {
                    throw new Exception(message);
                }
    
                message = string.Format("{0}\r\nIs exception: {1}\r\nMessage: {2}", 
                                        message, 
                                        response.Data.IsException, 
                                        response.Data.Message);
    
                if (response.Data.UserInfo == null)
                {
                    throw new Exception(message);
                }
    
                message = string.Format("{0}\r\nSiteName: {1}\r\nUserName: {2}\r\nUserRole: {3}", 
                                        message, 
                                        response.Data.UserInfo.SiteName, 
                                        response.Data.UserInfo.UserName, 
                                        response.Data.UserInfo.UserRole);
                if (response.Data.UserInfo.idUser == 0)
                {
                    throw new Exception(message);
                }
    
                return false;
            }

    Внимательность, внимательность и еще раз внимательность...

    HellMaster_HaiL, 26 Ноября 2014

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

    +135

    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
    public void ValidateValue( ref object value ) {
                bool b;
                if ( value == null ) return;
                var typeName = BaseType.Name;
                var valueType = value.GetType();
                if ( valueType.Name == "String" ) {
                    if ( BaseType == valueType ) return;
                    if ( BaseType == Constants.Types.Boolean ) {
                        if ( string.Compare( "yes", (string) value, StringComparison.OrdinalIgnoreCase ) == 0 ) value = true;
                        else if ( string.Compare( "no", (string) value, StringComparison.OrdinalIgnoreCase ) == 0 ) value = false;
    #if !CF
                        else if ( Boolean.TryParse( value.ToString(), out b ) ) value = b;
    #else
              else if (TryParseUtility.TryParse(value.ToString(), out b)) value = b;
    #endif
                        else throw new ArgumentException( String.Format( Resources.ValueNotCorrectType, value ) );
                        return;
                    }
                }
    
    #if !CF
                if ( typeName == "Boolean"
                     && Boolean.TryParse( value.ToString(), out b ) ) {
                    value = b;
                    return;
                }
    
                UInt64 uintVal;
                if ( typeName.StartsWith( "UInt64" )
                     && UInt64.TryParse( value.ToString(), out uintVal ) ) {
                    value = uintVal;
                    return;
                }
    
                UInt32 uintVal32;
                if ( typeName.StartsWith( "UInt32" )
                     && UInt32.TryParse( value.ToString(), out uintVal32 ) ) {
                    value = uintVal32;
                    return;
                }
    
                long intVal;
                if ( typeName.StartsWith( "long" )
                     && long.TryParse( value.ToString(), out intVal ) ) {
                    value = intVal;
                    return;
                }
    
                int intVal32;
                if ( typeName.StartsWith( "Int32" )
                     && Int32.TryParse( value.ToString(), out intVal32 ) ) {
                    value = intVal32;
                    return;
                }
    #else
          if (typeName == "Boolean" && TryParseUtility.TryParse(value.ToString(), out b)) { value = b; return; }
    
          UInt64 uintVal;
          if (typeName.StartsWith("UInt64") && TryParseUtility.TryParse(value.ToString(), out uintVal)) { value = uintVal; return; }
    
          UInt32 uintVal32;
          if (typeName.StartsWith("UInt32") && TryParseUtility.TryParse(value.ToString(), out uintVal32)) { value = uintVal32; return; }
    
          long intVal;
          if (typeName.StartsWith("long") && TryParseUtility.TryParse(value.ToString(), out intVal)) { value = intVal; return; }
    
          Int32 intVal32;
          if (typeName.StartsWith("Int32") && TryParseUtility.TryParse(value.ToString(), out intVal32)) { value = intVal32; return; }
    #endif
    
                object objValue;
    #if RT
          Type baseType = BaseType.GetTypeInfo().BaseType;
    #else
                var baseType = BaseType.BaseType;
    #endif
                if ( baseType != null
                     && baseType.Name == "Enum"
                     && ParseEnum( value.ToString(), out objValue ) ) {
                    value = objValue;
                    return;
                }
    
                throw new ArgumentException( String.Format( Resources.ValueNotCorrectType, value ) );
            }

    MySql.Data 6.9.5, MySqlConnectionStringBuilder.cs

    kasthack, 24 Ноября 2014

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

    +135

    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
    public void SelectStep(int stepNumber)
    {
        //1.HTTPCore
        //2.find cat
        //3find p.cat
        //4.find products
        //5.parse products info
        //6.save
        //7.complete
        if (stepNumber != 1) ((Label)(this.panel.Controls.Cast<Control>()
            .First(c => c.TabIndex == stepNumber - 1))).ForeColor = Color.Black;
        ((Label)(this.panel.Controls.Cast<Control>()
            .First(c => c.TabIndex == stepNumber))).ForeColor = Color.Red;
        if (stepNumber == 6)
        {
            labelStatusSecondLine.ForeColor = Color.Black;
            labelStatusFirstLine.Text = "Готово."; buttonStart.Enabled = buttonRefreshCats.Enabled = true; timer.Stop();
        }
        if (stepNumber == 7)
        {
            labelStatusSecondLine.Text = "Обновление категорий...";
        }
    }

    Досталось в наследство. Слегка переписано мной (ранее у всех лейблов были имена вроде "label1" – к лейблам аффтар обращался по распарсенным оттуда цифрам).

    pushistayapodmyshka, 20 Ноября 2014

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

    +135

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    static enum rc (*request_functions[])(void) = {
        ko,
        koko,
        kokoko,
        illegal_request
    };
    static inline enum rc illegal_request(void) { return ILLEGAL_REQUEST; }
    
    reply.rc = request_functions[cmd.opcode < NKEYS(request_functions) ? cmd.opcode : ILLEGAL_REQUEST]();

    Вызываем функцию по опкоду с абортом в случае index_out_of_bounds.

    codemonkey, 12 Ноября 2014

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

    +135

    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
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    main(){    
        
        FILE *filein, *fileout;
        
        char fname1[15], fname2[15];
        int n, m;
        int i, j;
        
        printf("Enter name of input file: "); scanf("%s", &fname1);
        printf("Enter name of output file: "); scanf("%s", &fname2);
        
        filein=fopen(fname1, "r");
        fileout=fopen(fname2, "w");
        if(filein==NULL){
          fprintf(stderr, "\nError: can't open file \"%s\"\n\n", fname1);
          fclose(filein);
          fclose(fileout);
          getch();
          }
        else{
             fscanf(filein, "%d%d", &n, &m);
             char ch[m][n];
             int array[m][n];
             int V[m][n];
             
             for(i=0;i<m;i++){
                for(j=0;j<n;j++){
                   V[i][j]=0;
                   }
                }
             
             for(i=0;i<m;i++){
                for(j=0;j<n;j++){
                   fscanf(filein, "%s", &ch[i][j]);
                   if(ch[i][j]=='+') array[i][j]=1;
                   else if(ch[i][j]=='-') array[i][j]=0;
                   }
                }
             
             int sum;
             for(i=0;i<m;i++){
                sum=0;
                for(j=0;j<n;j++){
                   sum+=array[i][j];
                   }
                   if(sum==1){
                             for(j=0;j<n;j++){
                                V[i][j]=array[i][j];
                                }
                             }
                   else continue;
                }
                
             int mm=0, c;
             for(i=0;i<m;i++){
                for(j=0;j<n;j++){
                   mm+=V[i][j];
                   }
                }
             c = m-mm;
             
             int VoteArray[n];
             
             for(j=0;j<n;j++){
                sum=0;
                for(i=0;i<m;i++){
                   sum+=V[i][j];
                   VoteArray[j]=sum;
                   }
                }
             
             float percent[n];
             float per;
             per = 100/(float)c;
             for(i=0;i<n;i++){
                
                if((percent[i]=per*VoteArray[i])>=7.0) fprintf(fileout, "%d ", i+1);
                }
                
             }
        
        fclose(fileout);
        fclose(filein);
        puts("\nMission comleted\nPress any key...\n");
        getch();
    }

    Фу, блять

    Sushev, 09 Ноября 2014

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

    +135

    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
    void *list_find_data(struct node *llist, void *data, int (*cmp_func)(void *, void *))
    {
    	struct node* it_node = NULL;
    
    	if (NULL != cmp_func)
    	{
    		for(it_node = llist; (NULL != it_node); it_node = it_node->next)
    	    {
                if (NULL != it_node->data)
                {
                    if (0 == cmp_func(data, it_node->data))
                    {
                        break;
                    }
                }
            }
    	}
    	return ((NULL != it_node) && (NULL != it_node->data)) ? it_node->data : NULL;
    }

    Велосипедисты жгут. О glibопараше не наслышаны.

    codemonkey, 04 Ноября 2014

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