-
Notifications
You must be signed in to change notification settings - Fork 426
Reduce memory footprint #2314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Reduce memory footprint #2314
Changes from all commits
5fc86e5
992a7a4
ad842e9
27ba70c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
| #endif | ||
|
|
||
| #include <string.h> | ||
| #include <sys/stat.h> | ||
| #include <fcntl.h> | ||
| #ifdef OS_WINDOWS | ||
| #include <io.h> | ||
|
|
@@ -163,6 +164,15 @@ | |
| } | ||
| } | ||
|
|
||
| void oscap_source_free_memory(struct oscap_source *source) | ||
| { | ||
| if (source != NULL) { | ||
| free(source->origin.memory); | ||
| source->origin.memory = NULL; | ||
| source->origin.memory_size = 0; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns human readable description of oscap_source origin | ||
| */ | ||
|
|
@@ -187,17 +197,80 @@ | |
| return reader; | ||
| } | ||
|
|
||
| xmlTextReader *oscap_source_get_streaming_xmlTextReader(struct oscap_source *source) | ||
| { | ||
| if (source->xml.doc != NULL) { | ||
| return oscap_source_get_xmlTextReader(source); | ||
| } | ||
|
|
||
| if (source->origin.memory != NULL) { | ||
| if (bz2_memory_is_bzip(source->origin.memory, source->origin.memory_size)) { | ||
| return oscap_source_get_xmlTextReader(source); | ||
| } | ||
| xmlTextReader *reader = xmlReaderForMemory(source->origin.memory, | ||
| source->origin.memory_size, NULL, NULL, 0); | ||
| if (reader == NULL) { | ||
| oscap_seterr(OSCAP_EFAMILY_XML, "Unable to create streaming xmlTextReader for %s", | ||
| oscap_source_readable_origin(source)); | ||
| oscap_setxmlerr(xmlGetLastError()); | ||
| } | ||
| return reader; | ||
| } | ||
|
|
||
| if (source->origin.filepath != NULL) { | ||
| int fd = open(source->origin.filepath, O_RDONLY); | ||
Check failureCode scanning / CodeQL Uncontrolled data used in path expression High
This argument to a file access function is derived from
user input (a command-line argument) Error loading related location Loading This argument to a file access function is derived from user input (a command-line argument) Error loading related location Loading This argument to a file access function is derived from user input (a command-line argument) Error loading related location Loading This argument to a file access function is derived from user input (a command-line argument) Error loading related location Loading This argument to a file access function is derived from user input (a command-line argument) Error loading related location Loading This argument to a file access function is derived from user input (a command-line argument) Error loading related location Loading This argument to a file access function is derived from user input (a command-line argument) Error loading related location Loading This argument to a file access function is derived from user input (a command-line argument) Error loading related location Loading |
||
| if (fd == -1) { | ||
| oscap_seterr(OSCAP_EFAMILY_XML, "Unable to open file for streaming xmlTextReader: %s", | ||
| oscap_source_readable_origin(source)); | ||
| return NULL; | ||
| } | ||
| if (bz2_fd_is_bzip(fd)) { | ||
| close(fd); | ||
| return oscap_source_get_xmlTextReader(source); | ||
| } | ||
| struct stat st; | ||
| if (fstat(fd, &st) != 0 || st.st_size <= 0) { | ||
| close(fd); | ||
| return oscap_source_get_xmlTextReader(source); | ||
| } | ||
| size_t file_size = (size_t)st.st_size; | ||
| source->origin.memory = malloc(file_size); | ||
| if (source->origin.memory == NULL) { | ||
| close(fd); | ||
| return oscap_source_get_xmlTextReader(source); | ||
| } | ||
| size_t total_read = 0; | ||
| while (total_read < file_size) { | ||
| ssize_t n = read(fd, source->origin.memory + total_read, file_size - total_read); | ||
| if (n <= 0) break; | ||
| total_read += (size_t)n; | ||
| } | ||
| close(fd); | ||
| source->origin.memory_size = total_read; | ||
| xmlTextReader *reader = xmlReaderForMemory(source->origin.memory, | ||
|
Comment on lines
+243
to
+250
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you considered using |
||
| source->origin.memory_size, source->origin.filepath, NULL, 0); | ||
| if (reader == NULL) { | ||
| oscap_seterr(OSCAP_EFAMILY_XML, "Unable to create streaming xmlTextReader for %s", | ||
| oscap_source_readable_origin(source)); | ||
| oscap_setxmlerr(xmlGetLastError()); | ||
| } | ||
| return reader; | ||
| } | ||
|
|
||
| oscap_seterr(OSCAP_EFAMILY_XML, "Unable to create streaming xmlTextReader for %s", | ||
| oscap_source_readable_origin(source)); | ||
| return NULL; | ||
| } | ||
|
|
||
| oscap_document_type_t oscap_source_get_scap_type(struct oscap_source *source) | ||
| { | ||
| if (source->scap_type == OSCAP_DOCUMENT_UNKNOWN) { | ||
| xmlTextReader *reader = oscap_source_get_xmlTextReader(source); | ||
| xmlTextReader *reader = oscap_source_get_streaming_xmlTextReader(source); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The CI fail looks valid. If we isolate the problem, it looks like it's caused by the change here. |
||
| if (reader == NULL) { | ||
| // the oscap error is already set | ||
| return OSCAP_DOCUMENT_UNKNOWN; | ||
| } | ||
| if (oscap_determine_document_type_reader(reader, &(source->scap_type)) == -1) { | ||
| oscap_seterr(OSCAP_EFAMILY_XML, "Unknown document type: '%s'", oscap_source_readable_origin(source)); | ||
| // in case of error scap_type must remain UNKNOWN | ||
| assert(source->scap_type == OSCAP_DOCUMENT_UNKNOWN); | ||
| } | ||
| xmlFreeTextReader(reader); | ||
|
|
@@ -385,7 +458,7 @@ | |
| const char *oscap_source_get_schema_version(struct oscap_source *source) | ||
| { | ||
| if (source->origin.version == NULL) { | ||
| xmlTextReader *reader = oscap_source_get_xmlTextReader(source); | ||
| xmlTextReader *reader = oscap_source_get_streaming_xmlTextReader(source); | ||
| if (reader == NULL) { | ||
| return NULL; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is never used anywhere. If it's intended for future use, please remove it until it's actually needed to avoid dead code.