Skip to content
Snippets Groups Projects
Commit cbcf0ff2 authored by Ettayeb Yassine's avatar Ettayeb Yassine
Browse files

cwe done

parent ee61bca1
No related branches found
No related tags found
No related merge requests found
File added
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
enum { OLD_SIZE = 10, NEW_SIZE = 20 };
int *resize_array(int *array, size_t count) {
if (0 == count) {
int *resize_array(int *array, size_t old_count, size_t new_count) {
if (0 == new_count) {
return 0;
}
int *ret = (int *)realloc(array, count * sizeof(int)); //@ split ret==0;
int *ret = (int *)realloc(array, new_count * sizeof(int));
if (!ret) {
free(array);
return 0;
}
if (new_count > old_count) {
memset(ret + old_count, 0, (new_count - old_count) * sizeof(int));
}
return ret;
}
void func(void) {
int *array = (int *)malloc(OLD_SIZE * sizeof(int));
if (0 == array) {
/* Handle error */
return;
}
for (size_t i = 0; i < OLD_SIZE; ++i) {
array[i] = i;
Frama_C_show_each_init_array(array[i]);
}
array = resize_array(array, NEW_SIZE);
array = resize_array(array, OLD_SIZE, NEW_SIZE);
if (0 == array) {
/* Handle error */
return;
}
for (size_t i = 0; i < NEW_SIZE; ++i) {
printf("%d ", array[i]);
}
......
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
void die(const char* s) {
printf("%s",s);
exit(2);
......@@ -24,10 +27,28 @@ int main () {
if ( EOF == error ){
die("No integer passed: Die evil hacker!\n");
}
// Vérification des dimensions pour éviter des erreurs de mémoire
if (m <= 0 || n <= 0 || m > MAX_DIM || n > MAX_DIM) {
die("Invalid board size: Die evil hacker!\n");
}
if ( m > MAX_DIM || n > MAX_DIM ) {
die("Value too large: Die evil hacker!\n");
}
//@ split m;
//@ split n;
board = (board_square_t*) malloc( m * n * sizeof(board_square_t));
// Vérification si malloc a échoué
if (!board) {
return 2;
}
//@ split m;
//@ split n;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
board[n*i+j] = 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment