Bonjour,
J'ai un exercice en programmation C et j'ai besoin d'aide pour le résoudre .
Merci d'avance
Voici l'exercice:
*)This C function performs its job most of the time, but has several defects (bugs, error handling, etc.) in specific situations.
Modify the code to make it as robust as possible, in order to make sure it will not crash or cause any problem in the application, even if the arguments provided to the function are incorrect.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 /* double_whitespaces: Copy the "msg" buffer of size "size" into the "dest" buffer, while doubling any blank character found in the source buffer. */ int double_whitespaces(char *msg, int size, char *dest) { int ret; char *ptr; char *buf; buf = malloc(size); ptr = msg; while (ptr < msg + size) { if (*ptr == '%') // % char is forbidden goto error; if (*ptr == ' ') { *buf++ = ' '; *buf++ = ' '; } else { *buf++ = *ptr; } ptr++; } free(buf); strcpy(dest, buf); return 0; error: return -1; }
Partager