{{tag>Brouillon}} # Notes langage C Voir : * [[Langage C sans compiler - Script en C]] * [GNU Coding Standards](https://www.gnu.org/prep/standards/html_node/Semantics.html) * https://developers.redhat.com/articles/2023/07/26/checking-usage-realloc-valgrind * GCC et RedHat : https://developers.redhat.com/articles/2023/11/10/install-gcc-and-build-hello-world-application-rhel-9 * https://developers.redhat.com/articles/2024/04/03/improvements-static-analysis-gcc-14-compiler * [C11 : la normalisation est achevée](https://www.developpez.com/actu/40233/C11-la-normalisation-est-achevee-apres-douze-ans-de-travaux-multithreading-et-Unicode-au-menu/) * https://pubs.opengroup.org/onlinepubs/009695399/ Voir aussi : * ISO C99 * https://developers.redhat.com/articles/2023/07/04/developers-guide-secure-coding-fortifysource * https://linuxfr.org/news/changement-de-licence-pour-mold-en-version-2-0 ~~~c #include #include #include #include int main(int argc, char **argv) { int i, size; #define SEPARATEUR " " char *cmdline; for (i=1; i < argc; i++) size += strlen(SEPARATEUR) + strlen(argv[i]); cmdline = malloc(size); if (cmdline ) { memset(cmdline, 0, size); for (i=1; i < argc; i++) { strcat(cmdline, argv[i]); strcat(cmdline, SEPARATEUR); } } printf("-- %s --", cmdline); system(cmdline); } ~~~ Erreur - implicit declaration of function ~~~ $ gcc plop.c plop.c: In function ‘main’: plop.c:29:5: error: implicit declaration of function ‘system’ [-Wimplicit-function-declaration] 29 | system(cmdline); | ^~~~~~ ~~~ Solution : Ajouter `#include ` Compilation ~~~bash gcc plop.c zig cc plop.c ~~~ ## Linter & format Formatage ~~~bash clang-format -i *.c ~~~ Linter ~~~bash clang-tidy plop.c ~~~ ## Surcharger un symbole Voir : https://stackoverflow.com/questions/2146059/limiting-syscall-access-for-a-linux-application Is the application linked statically? If not, you may override some symbols, for example, let's redefine socket: ~~~c int socket(int domain, int type, int protocol) { write(1,"Error\n",6); return -1; } ~~~ Then build a shared library: gcc -fPIC -shared test.c -o libtest.so Let's run: ~~~bash nc -l -p 6000 ~~~ Ok. And now: ~~~ $ LD_PRELOAD=./libtest.so nc -l -p 6000 Error Can't get socket ~~~ What happens when you run with variable LD_PRELOAD=./libtest.so? It overrides with symbols defined in libtest.so over those defined in the C library. ## Sécurité ~~~ $ man gets ... (DEPRECATED) Never use this function. BUGS Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead. For more information, see CWE-242 (aka "Use of Inherently Dangerous Function") at http://cwe.mitre.org/data/definitions/242.html ... ~~~ ### C double free prevention ~~~c // Source - https://stackoverflow.com/a/50786505 // Posted by Weather Vane // Retrieved 2026-05-07, License - CC BY-SA 4.0 char* ptr = malloc(sizeof(char)); *ptr = 'a'; free(ptr); ptr = NULL; // add this free(ptr); ~~~ ## Debug Valgrind et autres * -g : pour conserver les symboles de débogage et afficher les numéros de ligne précis * -O0 : pour désactiver les optimisations du compilateur ~~~bash gcc -g -O0 mon_programme.c -o mon_programme gcc -Wall -Wextra -Werror -ggdb -O0 -std=gnu17 -o hello_world hello_world.c ~~~ ~~~bash gcc -o executable -std=c11 -Wall main.c # Suppose it was this at first gcc -o executable -std=c11 -Wall -ggdb3 main.c # Add -ggdb3 to it ~~~ ### Valgrind Voir : * https://valgrind.org/docs/manual/quick-start.html * https://sourcesup.renater.fr/wiki/atelieromp/_media/atelier20180226_lroblou_valgrind.pdf Voir aussi : * heaptrack ~~~bash valgrind --leak-check=yes ./mon_programme valgrind --leak-check=full --show-leak-kinds=all ./mon_programme ~~~ ~~~bash valgrind --leak-check=full \ --show-leak-kinds=all \ --track-origins=yes \ --verbose \ --log-file=valgrind-out.txt \ ./executable exampleParam1 ~~~