myhtml
myhtml copied to clipboard
myhtml_serialization_tree_buffer is crash
1.program code,test.c
#include <stdio.h>
#include <stdlib.h>
#include <myhtml/api.h>
#include "example.h"
struct res_html {
char *html;
size_t size;
};
struct res_html load_html_file(const char* filename)
{
FILE *fh = fopen(filename, "rb");
if(fh == NULL) {
fprintf(stderr, "Can't open html file: %s\n", filename);
exit(EXIT_FAILURE);
}
if(fseek(fh, 0L, SEEK_END) != 0) {
fprintf(stderr, "Can't set position (fseek) in file: %s\n", filename);
exit(EXIT_FAILURE);
}
long size = ftell(fh);
if(fseek(fh, 0L, SEEK_SET) != 0) {
fprintf(stderr, "Can't set position (fseek) in file: %s\n", filename);
exit(EXIT_FAILURE);
}
if(size <= 0) {
fprintf(stderr, "Can't get file size or file is empty: %s\n", filename);
exit(EXIT_FAILURE);
}
char *html = (char*)malloc(size + 1);
if(html == NULL) {
fprintf(stderr, "Can't allocate mem for html file: %s\n", filename);
exit(EXIT_FAILURE);
}
size_t nread = fread(html, 1, size, fh);
if (nread != size) {
fprintf(stderr, "could not read %ld bytes (" MyCORE_FMT_Z " bytes done)\n", size, nread);
exit(EXIT_FAILURE);
}
fclose(fh);
struct res_html res = {html, (size_t)size};
return res;
}
int main(int argc, const char * argv[])
{
const char* path;
const char* path_insert;
if (argc == 3) {
path = argv[1];
path_insert = argv[2];
}
else {
printf("Bad ARGV!\nUse: serialization_high_level <path_to_html_file>\n");
exit(EXIT_FAILURE);
}
struct res_html res = load_html_file(path);
struct res_html res_insert = load_html_file(path_insert);
// basic init
myhtml_t* myhtml = myhtml_create();
mystatus_t status = myhtml_init(myhtml, MyHTML_OPTIONS_DEFAULT, 1, 0);
printf("status[%d]\n\n",status);
// init tree
myhtml_tree_t* tree = myhtml_tree_create();
myhtml_tree_init(tree, myhtml);
// parse html
myhtml_parse(tree, MyENCODING_UTF_8, res.html, res.size);
mycore_string_raw_t str_raw;
mycore_string_raw_clean_all(&str_raw);
myhtml_tree_node_t* node_insert = NULL;
myhtml_t* myhtml_insert = myhtml_create();
myhtml_init(myhtml_insert, MyHTML_OPTIONS_DEFAULT, 1, 0);
// init tree
myhtml_tree_t* tree_insert = myhtml_tree_create();
myhtml_tree_init(tree_insert, myhtml_insert);
// parse html
myhtml_parse(tree_insert, MyENCODING_UTF_8, res_insert.html, res_insert.size);
node_insert = myhtml_tree_get_node_html(tree_insert);
myhtml_tree_node_t* root = myhtml_tree_get_node_html(tree);
myhtml_tree_node_t* node_a = myhtml_node_create(tree, MyHTML_TAG_DIV, MyHTML_NAMESPACE_HTML);
myhtml_tree_node_add_child(node_a,node_insert);
myhtml_collection_t *collection = myhtml_get_nodes_by_tag_id(tree, NULL, MyHTML_TAG_FRAMESET, NULL);
//printf("collection[%p],length[%d]\n",collection,collection->length);
if(collection && collection->list && collection->length){
printf("----------------------------------------\n");
myhtml_tree_node_t *frame = collection->list[0];
myhtml_tree_node_insert_before(frame,node_a);
myhtml_node_delete(frame);
}
if(MyHTML_STATUS_OK == myhtml_serialization_tree_buffer(myhtml_tree_get_document(tree), &str_raw)) {
printf("%s", str_raw.data);
}
mycore_string_raw_destroy(&str_raw, false);
// release resources
myhtml_tree_destroy(tree);
myhtml_destroy(myhtml);
free(res.html);
return 0;
}
2.mk.sh
gcc test.c -g -o test -lmyhtml_static -lpthread -I/home/haima/myhtml/include -L/home/haima/myhtml/lib
- main.html,red.html code 3.1 main.html
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<base target="_blank">
<title>框架</title>
</head>
<frameset cols="50%,50%">
<frame src="red.html" name="redFrame">
</frameset>
<body>
test code test code test code
</body>
</html>
3.2 red.html
<html>
<head>
<title>redFrame</title>
<style>
body{ background-color: red; }
</style>
</head>
<body>
test content
</body>
</html>
<!--24256869360382548490090719-->
4.run parameter
issue description: ./test main.html red.html
program crash to myhtml_serialization_tree_buffer, when deleting the red.html last line(24256869360382548490090719-->),the program is running normally
The program crashes when the last action of the html page is commented
@kksvip hi! Thanks for report! I'll see it tomorrow.
@kksvip In fact, it is not possible to unite trees in this way. For this you must use innerHTML function, but in this project this function is not present.
Thank you very much, then I think about it again.