tech:notes_langage_c
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| tech:notes_langage_c [2025/10/05 02:24] – Jean-Baptiste | tech:notes_langage_c [2026/05/29 15:47] (Version actuelle) – Jean-Baptiste | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | < | ||
| + | {{tag> | ||
| + | |||
| + | # Notes langage C | ||
| + | |||
| + | Voir : | ||
| + | * [[Langage C sans compiler - Script en C]] | ||
| + | * [GNU Coding Standards](https:// | ||
| + | * https:// | ||
| + | * GCC et RedHat : https:// | ||
| + | * https:// | ||
| + | * [C11 : la normalisation est achevée](https:// | ||
| + | * https:// | ||
| + | |||
| + | Voir aussi : | ||
| + | * ISO C99 | ||
| + | * https:// | ||
| + | * https:// | ||
| + | |||
| + | ~~~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, | ||
| + | for (i=1; i < argc; i++) { | ||
| + | strcat(cmdline, | ||
| + | strcat(cmdline, | ||
| + | } | ||
| + | } | ||
| + | |||
| + | printf(" | ||
| + | |||
| + | system(cmdline); | ||
| + | |||
| + | } | ||
| + | ~~~ | ||
| + | |||
| + | Erreur - implicit declaration of function | ||
| + | ~~~ | ||
| + | $ gcc plop.c | ||
| + | plop.c: In function ‘main’: | ||
| + | plop.c: | ||
| + | 29 | | ||
| + | | | ||
| + | ~~~ | ||
| + | |||
| + | Solution : | ||
| + | Ajouter `#include < | ||
| + | |||
| + | |||
| + | Compilation | ||
| + | ~~~bash | ||
| + | gcc plop.c | ||
| + | zig cc plop.c | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
| + | ## Surcharger un symbole | ||
| + | |||
| + | Voir : | ||
| + | https:// | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | 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," | ||
| + | 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=./ | ||
| + | Error | ||
| + | Can't get socket | ||
| + | ~~~ | ||
| + | |||
| + | What happens when you run with variable LD_PRELOAD=./ | ||
| + | |||
| + | |||
| + | |||
| + | ## Sécurité | ||
| + | |||
| + | ~~~ | ||
| + | $ man gets | ||
| + | |||
| + | ... | ||
| + | (DEPRECATED) | ||
| + | Never use this function. | ||
| + | |||
| + | BUGS | ||
| + | | ||
| + | | ||
| + | |||
| + | For more information, | ||
| + | ... | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ### C double free prevention | ||
| + | |||
| + | ~~~c | ||
| + | // Source - https:// | ||
| + | // Posted by Weather Vane | ||
| + | // Retrieved 2026-05-07, License - CC BY-SA 4.0 | ||
| + | |||
| + | char* ptr = malloc(sizeof(char)); | ||
| + | |||
| + | *ptr = ' | ||
| + | free(ptr); | ||
| + | ptr = NULL; // add this | ||
| + | free(ptr); | ||
| + | ~~~ | ||
| + | |||
