Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.

Commit 00ad6af

Browse files
author
h4rl
committedMay 3, 2024··
feat(parse): Begin debugging leaks and a way to free the array in itself
1 parent e3af04d commit 00ad6af

File tree

8 files changed

+106
-33
lines changed

8 files changed

+106
-33
lines changed
 

‎.github/workflows/ossf.yml

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# This workflow uses actions that are not certified by GitHub. They are provided
2+
# by a third-party and are governed by separate terms of service, privacy
3+
# policy, and support documentation.
4+
5+
name: OSSF Scorecard analysis workflow
6+
on:
7+
push:
8+
branches:
9+
- main
10+
pull_request:
11+
branches:
12+
- main
13+
14+
permissions: read-all
15+
16+
jobs:
17+
analysis:
18+
name: Scorecard analysis
19+
runs-on: ubuntu-latest
20+
permissions:
21+
# Needed if using Code scanning alerts
22+
security-events: write
23+
# Needed for GitHub OIDC token if publish_results is true
24+
id-token: write
25+
26+
steps:
27+
- name: "Checkout code"
28+
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
29+
with:
30+
persist-credentials: false
31+
32+
- name: "Run analysis"
33+
uses: ossf/scorecard-action@0864cf19026789058feabb7e87baa5f140aac736 # v2.3.1
34+
with:
35+
results_file: results.sarif
36+
results_format: sarif
37+
publish_results: true
38+
39+
# Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF
40+
# format to the repository Actions tab.
41+
- name: "Upload artifact"
42+
uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # v4.3.3
43+
with:
44+
name: SARIF file
45+
path: results.sarif
46+
retention-days: 5
47+
48+
# required for Code scanning alerts
49+
- name: "Upload to code scanning"
50+
uses: github/codeql-action/upload-sarif@1b1aada464948af03b950897e5eb522f92603cc2 # v3.24.9
51+
with:
52+
sarif_file: results.sarif

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# exMeteo
22

33
![[LICENSE](https://github.com/h4rldev/exmeteo/blob/main/LICENSE)](https://img.shields.io/github/license/h4rldev/exmeteo?style=flat-square)
4+
![[OpenSSF Scorecard](https://securityscorecards.dev/viewer/?uri=github.com/h4rldev/exmeteo)](https://api.securityscorecards.dev/projects/github.com/h4rldev/exmeteo/badge?style=flat-square)
45

56

67
> [!CAUTION]

‎justfile

-1
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,3 @@ default:
2222
@debug name="exmeteo":
2323
just compile-debug {{name}}
2424
just link {{name}}
25-
gdb ./bin/{{name}}

‎src/exmeteo/cache.c

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "cache.h"
2+
3+
cJSON *read_from_cache(FILE *cache);
4+
5+
int write_to_json(char* filename, cJSON *json) {
6+
return 0;
7+
}

‎src/exmeteo/cache.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef CACHE_H_INCLUDED
2+
#define CACHE_H_INCLUDED
3+
4+
#include <cjson/cJSON.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
8+
#ifdef _WIN32
9+
10+
#define CACHE_DIR() getenv("APPDATA") "\\exmeteo\\"
11+
12+
#elif __linux__
13+
14+
#define CACHE_DIR() getenv("HOME") "/.cache/exmeteo/"
15+
16+
#else
17+
#error "Unsupported Operating System, use a normal os, loser"
18+
#endif
19+
20+
cJSON *read_from_cache(FILE *cache);
21+
22+
int write_to_json(char *filename, cJSON *json);
23+
int update_cache(char *api_key);
24+
int clear_cache();
25+
26+
#endif

‎src/exmeteo/cli.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cli.h"
22
#include "parse.h"
3+
#include <curl/curl.h>
34
#include <stdio.h>
45

56
#ifdef _WIN32
@@ -64,7 +65,7 @@ const char* flags[10] = {
6465
"--version", // 3 version-flag
6566
"-v", // 4 shorterm for version
6667
"--convert-currency", // 5 currency-converter flag
67-
"--currenct-convert", // 6 currency-converter flag
68+
"--currency-convert", // 6 currency-converter flag
6869
"--convert", // 7 shorterm for currency-converter
6970
"--currency", // 8 shorterm for currency-converter
7071
"-c", // 9 extra shorterm for currency-converter
@@ -123,9 +124,11 @@ int print_help(char **argv[]) {
123124

124125
char* help[3] = {"--help", "-h", "-?"};
125126
char* version[2] = {"--version", "-v"};
127+
char* currency[5] = {"--convert-currency", "--currency-convert", "--convert", "--currency", "-c"};
126128

127129
info_for_flag(help, 3, GREEN, "Prints this message.", YELLOW);
128130
info_for_flag(version, 2, GREEN, "Prints program version.", YELLOW);
131+
info_for_flag(currency, 5, GREEN, "Convert one currency to another", YELLOW);
129132

130133
print_line("-");
131134

@@ -143,6 +146,7 @@ int print_version(void) {
143146
int init(int argc, char *argv[]) {
144147
char *user = getUsername();
145148

149+
146150
int flag = compare_flags(argv);
147151
switch (flag) {
148152
case 0:
@@ -163,7 +167,11 @@ int init(int argc, char *argv[]) {
163167
char *api_key = "966eb565013e92b110e1cf0d";
164168
char ***array = currency__get_codes(api_key);
165169
printf("got array hopefully\n");
166-
printf("%s", array[0][1]);
170+
int array_length = sizeof(array) / sizeof(array[0]);
171+
printf("array len = %d", array_length);
172+
printf("%s \n", array[0][1]);
173+
free_2D_string_array(array, 162);
174+
printf("size of char *: %lu \nsize of char **: %lu \nsize of char ***: %lu", sizeof(char *), sizeof(char **), sizeof(char ***));
167175
break;
168176
};
169177

‎src/exmeteo/parse.c

+8-29
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ cJSON *currency__get_json_value(char* api_key, char* value) {
1010

1111
char *response = req(url);
1212
size_t response_length = (size_t)strlen(response);
13+
printf("response length: %lu", response_length);
1314

1415
cJSON *json = cJSON_ParseWithLength(response, response_length);
1516
if (! json) {
@@ -19,28 +20,11 @@ cJSON *currency__get_json_value(char* api_key, char* value) {
1920
}
2021
return 0;
2122
}
22-
cJSON *json_value = cJSON_GetObjectItemCaseSensitive(json, value);
23+
cJSON *json_value = cJSON_GetObjectItemCaseSensitive(json, value);
24+
free(response);
2325
return json_value;
2426
}
2527

26-
char ***allocate_2D_string_array(int totalStrings, int stringSize) {
27-
// Allocate memory for the outer array
28-
printf("allocating %lu to array\n", totalStrings * sizeof(char **));
29-
char ***stringList = (char ***)malloc(totalStrings * sizeof(char **));
30-
31-
// Allocate memory for each inner array
32-
for (int i = 0; i < totalStrings; i++) {
33-
stringList[i] = (char **)malloc((2 * sizeof(char*)));
34-
35-
// Allocate memory for each string
36-
for (int j = 0; j < 2; j++) {
37-
stringList[i][j] = (char *)malloc(stringSize);
38-
}
39-
}
40-
41-
return stringList;
42-
}
43-
4428
void free_2D_string_array(char ***stringList, int totalStrings) {
4529
for (int i = 0; i < totalStrings; i++) {
4630
for (int j = 0; j < 2; j++) {
@@ -63,9 +47,8 @@ char*** currency__get_codes(char *api_key) {
6347
}
6448
// Determine the size of the JSON array
6549
int size = cJSON_GetArraySize(codes);
66-
6750
// Allocate memory for the 2D array
68-
char ***currency_codes = (char ***)malloc((size * sizeof(char ***)));
51+
char ***currency_codes = (char ***)malloc((size * sizeof(char **)));
6952
// Iterate over the JSON array and store the strings in the 2D array
7053
for (int i = 0; i < size; i++) {
7154
cJSON *item = cJSON_GetArrayItem(codes, i);
@@ -78,28 +61,24 @@ char*** currency__get_codes(char *api_key) {
7861
int code_size = (strlen(code_str) + 1);
7962
int name_size = (strlen(name_str) + 1);
8063

81-
currency_codes[i] = (char **)malloc((2 * sizeof(char **)));
64+
currency_codes[i] = (char **)malloc((2 * sizeof(char *)));
8265
printf("Allocating currency_codes[%d][0]..", i);
8366
currency_codes[i][0] = (char *)malloc(code_size); // +1 for the null terminator
8467
printf("Allocating currency_codes[%d][1]..", i);
8568
currency_codes[i][1] = (char *)malloc(name_size); // +1 for the null terminator
8669

70+
8771
strncpy(currency_codes[i][0], code_str, code_size);
8872
strncpy(currency_codes[i][1], name_str, name_size);
89-
cJSON_free(item);
90-
cJSON_free(code);
91-
cJSON_free(name);
92-
cJSON_free(code_str);
93-
cJSON_free(name_str);
9473
}
9574

9675
// Print the 2D array to verify
97-
//for (int i = 0; i < size; i++) {
76+
// for (int i = 0; i < size; i++) {
9877
// printf("[\"%s\",\"%s\"]\n", currency_codes[i][0], currency_codes[i][1]);
9978
//}
10079

10180
// Free the allocated memory
102-
cJSON_Delete(codes);
81+
cJSON_Delete(codes);
10382
return currency_codes;
10483
}
10584

‎src/exmeteo/req.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
3030

3131
char* req(char* url) {
3232
CURL *curl;
33-
char *resp;
33+
char *resp = 0;
3434
CURLcode response;
3535

3636
struct MemoryStruct chunk;
@@ -65,6 +65,7 @@ char* req(char* url) {
6565
}
6666

6767
strncpy(resp, chunk.memory, chunk.size);
68+
free(chunk.memory);
6869
curl_easy_cleanup(curl);
6970
curl_global_cleanup();
7071
return resp;

0 commit comments

Comments
 (0)
This repository has been archived.