Skip to content

Commit

Permalink
Avoid pointers to literal strings.
Browse files Browse the repository at this point in the history
Also, some changes and comments to be safe.
  • Loading branch information
J-Rios authored and JuanjoSalvador committed Jun 2, 2023
1 parent e2f39ad commit 27add84
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/dog.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -7,15 +8,20 @@
#include "functions.h"
#include "dogos.h"

static const uint8_t MAX_SIZE_BARK = 64;
static const uint8_t MAX_SIZE_VERSION = 24;
static const uint8_t MAX_SIZE_RENDER = 64;

int main(int argc, char *argv[]) {
char* bark = "Whoof!";
char dog_version[255];

char bark[MAX_SIZE_BARK];
char dog_version[MAX_SIZE_VERSION];
int opt;
int long_index = 0;
int length = 2;

sprintf(dog_version, "Dog, v%s", version);
snprintf(bark, MAX_SIZE_BARK, "%s", "Whoof!");
snprintf(dog_version, MAX_SIZE_VERSION, "Dog, v%s", version);

static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
Expand Down Expand Up @@ -105,7 +111,7 @@ void render(char* bark, int finish, int length) {
} else {
sprintf(buffer, "%s%s%s %s\n", dog_bag[i], middle, dog_front[i], bark);
}
printf("%s", buffer);
printf("%s\n", buffer);
}
if(finish == true) {
exit(0);
Expand Down Expand Up @@ -134,6 +140,17 @@ void renderFile(char* filepath) {
exit(EXIT_SUCCESS);
}

// Note: This Function is not used
//
// Note: In this function you are allocating dynamic memory multiple times.
// You must free this memory, it is like constantly kick the dog, at
// some point, the dog will bite you. Also, avoid returning a
// dynamically allocated data outside the function, nobody outside know
// it is dynamic and must free it.
//
// Note: strcpy(), strcat(), sprintf() functions are unsafe (it easy to write
// outside the reserved memory using it), it is better to use a safest
// functions like strncpy(), stncat() and snprintf().
char* paramsToString(int argc, char *argv[]) {
if(argc < 1) {
return "";
Expand All @@ -145,10 +162,14 @@ char* paramsToString(int argc, char *argv[]) {
if(argc > 1) {
for(int i = 2; i < argc; i++) {
char* tmp = (char*) malloc(strlen(ret) + strlen(argv[i]) + 1);
// You should check if the allocation fails
//if (tmp == NULL)
// continue;
strcpy(tmp, ret);
strcat(tmp, " ");
strcat(tmp, argv[i]);
strcpy(ret, tmp);
//free(tmp); // You must free this in each iteration
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ void render(char* bark, int finish, int length);
void renderFile(char* filepath);
char* paramsToString(int argc, char *argv[]);

// A header file shouldn't implements functions, just define the function
// prototype/interface (this function should be moved to dog.c)
void clearScreen()
{
const char *CLEAR_SCREEN_ANSI = "\e[1;1H\e[2J";
Expand Down

0 comments on commit 27add84

Please sign in to comment.