What was the most dangerous programming mistake you have made in C?

前端 未结 26 1250
南方客
南方客 2021-02-03 12:46

I am an intermediate C programmer. If you have made any coding mistake that you came to know later that it was the most hazardous / harmful to the total application please share

26条回答
  •  终归单人心
    2021-02-03 13:32

    I take the definition of dangerous as "we may ship with that bug and discover only years later when it's to late":

    char* c = malloc(...);
    .
    .
    .
    free(c);  
    .
    .
    .
    c[...] = ...; 
    

    or

    // char* s is an input string
    char* c = malloc(strlen(s));
    strcpy(c, s);
    

    But if you write multiplatform (not limited to x86/x64) this is also great:

    char* c = ...;
    int i = *((int*)c); // <-- alignment fault
    

    And if your buffer comes from an untrusted source.. basically most code around is dangerous.

    But, anyway, in C it's so easy to shoot yourself in the foot, that a topic about shot feet could go around the thousands of pages.

提交回复
热议问题