text
stringlengths
0
7.83k
h = h->next;
l++;
return l;
int main(int argc, char *argv[static argc+1) {
(void)argc;
int mypid = getpid();
printf("My PID is: %d. Catch me with 'gdb %s %d' !\n", mypid, argv[0], mypid);
// build a short and smallcircular list in stack
Elem a;
Elem b = {.next = &a};
a = (Elem){.next = &b};
Elem *head = &a;
// infinite loop in list_length
printf("list length: %lu\n", list_length(head));
return EXIT_SUCCESS;
Pour éviter des mésaventures désagréables, comme vider la batterie de
son ordinateur portable, nous conseillons de toujours lancer ce
programme en limitant son temps d'exécution (ici à 300 secondes, 5
minutes max). Attention à bien mettre les parenthèses, sinon vous
risquez de limiter aussi tous les processus que vous lancerez dans le
même terminal.
$ ( ulimit -t 300; ./bug_loop ) &
Les consignes.
Le programme tourne donc à l'infini. Vous avez donc tout le temps pour
l'attraper au vol. Pour faciliter votre travail, il indique aussi
comment l'attraper au vol avec votre débogueur. L'alternative est
d'obtenir le PID du processus avec la commande codice_1.
Après l'avoir attraper, vous changer la valeur d'une de ces variables
à la volée pour lui permettre de terminer proprement.
Débogage avancé/Travail pratique/Observation à la loupe
But de ce TP.
Un des bugs classiques en C et C++ est de lire ou d'écrire en dehors d'une zone préalablement allouée. Suivant les cas les comportements à l'exécution sont un peu différents. Cela va permettre d'illustrer différentes techniques sur des bugs très similaires.
Les trois codes à déboguer.
Lire un peu avant la zone allouée.
Créer un fichier "bug_overreadheap_tiny.c" contenant le code suivant.
const uint32_t NB = 100;
void fibon(const uint32_t n, uint32_t p[static n]) {
// C11 array args, p is guaranteed by programmer to be at least n
for (uint32_t i = 0; i < n; i++)
if (i < 1) // BUG: i < 2
p[i] = i;
else
p[i] = p[i - 1] + p[i - 2]; // BUG: read p[-1]
int main() {
assert(NB > 2);
uint32_t *p = malloc(sizeof(uint32_t[NB]));
assert(p != NULL);
memset(p, 0, sizeof(uint32_t[NB]));
fibon(NB, p);
free(p);
return EXIT_SUCCESS;
Écrire un peu après la zone allouée.
Créer un fichier "bug_overwriteheap_tiny.c" contenant le code suivant.
const unsigned int NB = 10000;
void fibon(unsigned int size, unsigned int p[size]) {
for (unsigned int i = 0; i <= size; i++) // BUG: i < size
if (i < 2)
p[i] = i;
else
p[i] = p[i - 1] + p[i - 2]; // BUG: write p[NB]
int main() {
assert(NB > 2);
unsigned int *p = malloc(sizeof(int[NB]));
assert(p != NULL);
fibon(NB, p);
free(p);
return EXIT_SUCCESS;
Écrire loin au-delà de la zone allouée.
Créer un fichier "bug_overwriteheap_large.c" contenant le code suivant.
const unsigned int SIZE = 10000; // size may be a misleading name
fibon overwrite the array 4 times the length allocated by main
Thanks to the C11 array size notation, gcc-12+ detects the bug and
warns you ! Read the warnings ! Solve them !
Use as recent as possible compiler. Even for quite stable language
as C, it is helpful.
Note also that with the old classical notation, aka.
unsigned int size, unsigned int *p
or if you do not indicate the size of the array in the argument,
the compiler may not help you.
@param size size of the array
@param p array of size "size"
void fibon(unsigned int size, unsigned int p[static size]) {
for (unsigned int i = 0; i <= size; i++)
if (i < 2)
p[i] = i;
else
p[i] = p[i - 1] + p[i - 2];
int main() {
assert(SIZE > 2);
unsigned int *p = malloc(SIZE); // BUG: 4x smaller than correct sizeof( int[SIZE] ));
assert(p != NULL);
fibon(SIZE, p);
free(p);
return 0;
Les consignes.
Lire un peu avant la zone allouée.