1. Си / Говнокод #17154

    +133

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    int size;
    
    size = EXPR;
    
    if (size > INT_MAX || size <= 0) {
        return NULL;
    }
    
    // ...

    Ндя. Семь лет уже. Теперь всё понятно...

    bot, 23 Ноября 2014

    Комментарии (52)
  2. Си / Говнокод #17150

    +139

    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
    SGELISTENTRY *sgeListAdd(SGELIST *l, const char *id, void *data) {
       SGELISTENTRY *ret;
    
       sgeNew(ret, SGELISTENTRY);
       l->numberOfEntries++;
       if (l!=NULL) {
          ret->prev=l->last;
       } else {
          ret->prev=NULL;
       }
       if (l!=NULL && l->last!=NULL) {
          l->last->next=ret;
       }
       ret->next=NULL;
       ret->id=strdup(id);
       ret->data=data;
    
       if (l==NULL) return ret;
    
       if (l->first==NULL) l->first=ret;
       l->last=ret;
    
       return ret;
    }

    Эх, проверяй, не проверяй, один хрен все грохнется при l==NULL

    Pythoner, 21 Ноября 2014

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

    +136

    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
    len += sprintf(event_xml_msg, XML_TAG_START, XML_KOKOKO_HTTP_PROTOCOL);
    
    	// Set <monitor-event>
    	len += sprintf(strend_ptr(event_xml_msg), XML_TAG_START, XML_MONITOR_EVENT_NODE_TREE);
    
    	// Set <date>
    	len += xml_string_add_tag(event_xml_msg, XML_MONITOR_EVENT_NODE_DATE, dt.date_b);
    
    	// Set <time>
    	len += xml_string_add_tag(event_xml_msg, XML_MONITOR_EVENT_NODE_TIME, dt.time_b);
    
    	// Set <product> Ex. "VersAtive"
    	len += xml_string_add_tag(event_xml_msg, XML_MONITOR_EVENT_NODE_PRODUCT, product_type);
    
    	// Set <entity code>
    	// Supposed to work for all union types
    	len += xml_int_add_tag(event_xml_msg, XML_MONITOR_EVENT_NODE_CODE, event_code);
    
    	// Set <severity>
    	//	len += xml_int_add_tag(event_xml_msg, XML_MONITOR_EVENT_NODE_SEVERITY, severity);
    	memset(severity_str, 0, sizeof(severity_str));
    	get_severity_string(severity, severity_str);
    	len += xml_string_add_tag(event_xml_msg, XML_MONITOR_EVENT_NODE_SEVERITY, severity_str);
    
    	// Set event entity name
    	len += xml_string_add_tag(event_xml_msg, XML_MONITOR_EVENT_NODE_ENTITY_TYPE, entity_name);
    
    	// Set event description
    	if((len + strlen(description)) > (payload_size - footer_size))
    	{
    		// TODO HANDLE
    		printf("Message description overflows buffer size.\n");
    		return false;
    	}
    	len += xml_cdata_string_add_tag(event_xml_msg, XML_MONITOR_EVENT_NODE_DESCRIPTION, description);
    
    	// Set params
    	add_xml_entity_params(event_xml_msg, entity_params);
    
    	// Close <monitor-event>
    	sprintf(strend_ptr(event_xml_msg), XML_TAG_END, XML_MONITOR_EVENT_NODE_TREE);
    
    	// Close <HTTPProtocol>
    	len += sprintf(strend_ptr(event_xml_msg), XML_TAG_END, XML_KOKOKO_HTTP_PROTOCOL);

    В проекте широко используется libmxml, а вот блять использовать его по назначению велосипедики не могут.

    codemonkey, 20 Ноября 2014

    Комментарии (56)
  4. Си / Говнокод #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)
  5. Си / Говнокод #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)
  6. Си / Говнокод #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)
  7. Си / Говнокод #16887

    +131

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    struct tm * localtime (const time_t * timer);
    
    A pointer to a tm structure with its members filled with the values
     that correspond to the local time representation of timer.
    
     The returned value points to an internal object whose validity or
     value may be altered by any subsequent call to gmtime or localtime.

    Я нуб, впервые вижу такой способ вернуть структуру.

    TarasB, 18 Октября 2014

    Комментарии (24)
  8. Си / Говнокод #16854

    +133

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    if (strcmp(request_name, REQUEST_1) == 0)
                function_1();
            else if (strcmp(request_name, REQUEST_2) == 0)
                function_2();
            else if (strcmp(request_name, REQUEST_3) == 0)
                function_3();
            /* И так далее */
            else if (strcmp(request_name, REQUEST_N) == 0)
                function_n();

    Собственно, как можно ЭТО рефакторить в более приемлемый вид и можно ли заменить на branchless?

    codemonkey, 15 Октября 2014

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

    +133

    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
    /*Checks whether the path exists and the path is directory, otherwise creates a new directory*/
    RET_VALS check_create_directory(const char* const dir_path, void (*print_func)(int, char *,...))
    {
    	RET_VALS ret_val;
    	struct stat sb;
    
    	ret_val = RET_OK;
    	if (NULL != dir_path)
    	{
    		if(NULL != print_func)
    		{
    			print_func(DBG_INFO, "%s - Checking %s existence\n", __FUNCTION__, dir_path);
    		}
    		else
    		{
    			dbgprintln("Checking %s existence", dir_path);
    		}
    		if (0 != stat(dir_path, &sb) || (false == S_ISDIR(sb.st_mode)))
    		{
    			ret_val |= RET_DIR_MISSING;
    			if(NULL != print_func)
    			{
    				print_func(DBG_INFO, "%s - %s is missing\n", __FUNCTION__, dir_path);
    			}
    			else
    			{
    				dbgprintln("%s is missing", dir_path);
    			}
    			errno = 0;
    			if (0 == mkdir(dir_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))
    			{
    				ret_val |= RET_DIR_CREATE_OK;
    				if(NULL != print_func)
    				{
    					print_func(DBG_INFO, "%s - %s is successfully created\n", __FUNCTION__, dir_path);
    				}
    				else
    				{
    					dbgprintln("%s is successfully created", dir_path);
    				}
    			}
    			else
    			{
    				char err_msg[_K];
    
    				sprintf(err_msg, "Failed to create %s, error %04d - %s", dir_path, errno, strerror(errno));
    				ret_val |= RET_DIR_CREATE_FAILED;
    				//dbg_ffln(DBG_ERROR, "Failed to create %s", dir_path);
    				if(NULL != print_func)
    				{
    					print_func(DBG_INFO, "%s - %s\n", __FUNCTION__, err_msg);
    				}
    				else
    				{
    					dbgprintln("%s", err_msg);
    				}
    			}
    		}
    		else
    		{
    			ret_val |= RET_DIR_ALREADY_EXIST;
    			if(NULL != print_func)
    			{
    				print_func(DBG_INFO, "%s - %s already exists\n", __FUNCTION__, dir_path);
    			}
    			else
    			{
    				dbgprintln("%s already exists", dir_path);
    			}
    		}
    	}
    	else
    	{
    		ret_val = RET_DIR_MISSING;
    		if(NULL != print_func)
    		{
    			print_func(DBG_ERROR, "%s - No directory name is provided", __FUNCTION__);
    		}
    		else
    		{
    			dbgprintln("No directory name is provided");
    		}
    	}
    	return ret_val;
    }

    Продолжаем раскопки. Вообще весь .с файл можно сюда выложить.

    codemonkey, 12 Октября 2014

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

    +134

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    #ifdef __APPLE__
    #define OPENGL_LIBRARY "/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib"
    #else
    #define OPENGL_LIBRARY "libGL.so"
    #endif

    А вообще, у мака есть переменные окружения?

    Pythoner, 10 Октября 2014

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