Skip to content
Snippets Groups Projects
Commit 75d83bf1 authored by Jonathan Schöbel's avatar Jonathan Schöbel
Browse files

NodeFragment: changed semantics of insert_attr_new

When there are methods in the api/abi, that take pointers to strings to
store them in the library, there are two methods to do so. Either they
are copying the string and leaving it intact, or they directly assign
the given pointer to some internal storage. While the former method, is
safer in terms of memory, as the user doesn't have to remember that he
can't use the string anymore, the latter can be more efficient, as there
is no extra strdup call, but the user is not allowed to change the
pointer, free it and also can't use the pointer, because it can't be
known whether it is already freed by the library. As it should be
decideable by the user, the library often implements both approaches,
where the method, that directly store pointers without creating a copy
contains the raw_ prefix.

The insert_attr_new methods were somewhat confusing in this regard, as
it is likely to be expected, that they were copying the string, while in
fact they did not. This is now fixed. The insert_attr_new methods copy
the strings, while the added insert_attr_raw_new methods preserve the
old behaviour of the insert_attr_new methods, thus allowing to let the
user choose, what is more appropriate.
parent 112b8438
No related branches found
No related tags found
No related merge requests found
......@@ -43,8 +43,8 @@ FILE_NAME_10=1550;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fp
FILE_NAME_11=1562;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Flib%2Fsefht%2Ffragment.h;0;8
FILE_NAME_12=2022;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Flib%2Fsefht%2Ffragment_data.c;0;8
FILE_NAME_13=2558;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Flib%2Fsefht%2Ffragment_class.c;0;8
FILE_NAME_14=9423;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Flib%2Fsefht%2Fnode_fragment.c;0;8
FILE_NAME_15=6811;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Flib%2Fsefht%2Fnode_fragment.h;0;8
FILE_NAME_14=12922;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Flib%2Fsefht%2Fnode_fragment.c;0;8
FILE_NAME_15=6478;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Flib%2Fsefht%2Fnode_fragment.h;0;8
FILE_NAME_16=4590;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Flib%2Fsefht%2Fattr.c;0;8
FILE_NAME_17=2828;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Flib%2Fsefht%2Fattr.h;0;8
FILE_NAME_18=3893;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Fsrc%2Flib%2Fsefht%2Fattr_static.c;0;8
......@@ -63,7 +63,7 @@ FILE_NAME_30=555;Make;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2
FILE_NAME_31=218;Sh;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2Fno_test.sh.in;0;8
FILE_NAME_32=1085;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2Ftest_cms.c;0;8
FILE_NAME_33=3283;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2Ftest_data.c;0;8
FILE_NAME_34=28347;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2Ftest_node_fragment.c;0;8
FILE_NAME_34=126847;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2Ftest_node_fragment.c;0;8
FILE_NAME_35=11319;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2Ftest_attr.c;0;8
FILE_NAME_36=11068;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2Ftest_text.c;0;8
FILE_NAME_37=5744;C;0;EUTF-8;1;1;0;%2Fhome%2Fjonathan%2FDokumente%2Fprojekte%2Fprgm%2Finternet%2Fweb%2FSeFHT%2Ftests%2Ftest_validator.c;0;8
......
......@@ -617,13 +617,81 @@ insert_attr (struct SH_NodeFragment * fragment,
static inline
bool
insert_attr_new (struct SH_NodeFragment * fragment,
/*@only@*/ char * name,
/*@null@*/ /*@only@*/ char * value,
const char * name,
/*@null@*/ const char * value,
size_t position,
/*@out@*/ /*@null@*/ struct SH_Status * status)
/*@modifies fragment->attrs@*/
/*@modifies fragment->attr_s@*/
/*@modifies fragment->attr_n@*/
/*@globals fileSystem@*/
/*@modifies fileSystem@*/
/*@modifies status@*/
{
size_t new_size;
size_t index;
struct SH_Attr attr;
typeof (fragment->attrs) new_attrs;
if (!Attr_init (&attr, name, value, status))
{
return FALSE;
}
new_size = get_attr_alloc_size (fragment->attr_n + 1);
if (new_size > fragment->attr_s)
{
if ((SIZE_MAX / sizeof (*new_attrs)) < new_size)
{
set_status (status, E_DOMAIN, -6,
"maximum number of "
"attrs reached.\n");
Attr_free (&attr);
return FALSE;
}
new_attrs = realloc (fragment->attrs, new_size
* sizeof (*new_attrs));
if (new_attrs == NULL)
{
set_status (status, E_ALLOC, 5,
"malloc failed\n");
/* bad code to silence splint, should never be executed. */
#ifdef S_SPLINT_S
fragment->attrs = (void *) 0x12345;
#endif
Attr_free (&attr);
return FALSE;
}
fragment->attrs = new_attrs;
fragment->attr_s = new_size;
}
for (index = fragment->attr_n; index > position; index--)
{
fragment->attrs[index] = fragment->attrs[index-1];
}
Attr_move (&(fragment->attrs[position]), &attr);
fragment->attr_n++;
set_success (status);
return TRUE;
}
static inline
bool
insert_attr_raw_new (struct SH_NodeFragment * fragment,
/*@only@*/ char * name,
/*@null@*/ /*@only@*/ char * value,
size_t position,
/*@out@*/ /*@null@*/ struct SH_Status * status)
/*@modifies fragment->attrs@*/
/*@modifies fragment->attr_s@*/
/*@modifies fragment->attr_n@*/
/*@modifies name@*/
/*@modifies value@*/
/*@globals fileSystem@*/
......@@ -769,8 +837,8 @@ SH_NodeFragment_insert_attr (struct SH_NodeFragment * fragment,
bool
SH_NodeFragment_insert_attr_new (struct SH_NodeFragment * fragment,
/*@only@*/ char * name,
/*@null@*/ /*@only@*/ char * value,
const char * name,
/*@null@*/ const char * value,
size_t position,
/*@out@*/ /*@null@*/
struct SH_Status * status)
......@@ -791,6 +859,31 @@ SH_NodeFragment_insert_attr_new (struct SH_NodeFragment * fragment,
return insert_attr_new (fragment, name, value, position, status);
}
bool
SH_NodeFragment_insert_attr_raw_new (struct SH_NodeFragment * fragment,
/*@only@*/ char * name,
/*@null@*/ /*@only@*/ char * value,
size_t position,
/*@out@*/ /*@null@*/
struct SH_Status * status)
/*@modifies fragment->attrs@*/
/*@modifies fragment->attr_s@*/
/*@modifies fragment->attr_n@*/
/*@globals fileSystem@*/
/*@modifies fileSystem@*/
/*@modifies status@*/
{
if (position > fragment->attr_n)
{
set_status (status, E_VALUE, 2,
"index out of range.\n");
return FALSE;
}
return insert_attr_raw_new (fragment, name, value, position,
status);
}
bool
SH_NodeFragment_insert_attr_copy (struct SH_NodeFragment * fragment,
const SH_Attr * attr,
......@@ -831,8 +924,8 @@ SH_NodeFragment_prepend_attr (struct SH_NodeFragment * fragment,
bool
SH_NodeFragment_prepend_attr_new (struct SH_NodeFragment * fragment,
/*@only@*/ char * name,
/*@null@*/ /*@only@*/ char * value,
const char * name,
/*@null@*/ const char * value,
/*@out@*/ /*@null@*/
struct SH_Status * status)
/*@modifies fragment->attrs@*/
......@@ -845,6 +938,22 @@ SH_NodeFragment_prepend_attr_new (struct SH_NodeFragment * fragment,
return insert_attr_new (fragment, name, value, 0, status);
}
bool
SH_NodeFragment_prepend_attr_raw_new (struct SH_NodeFragment * fragment,
/*@only@*/ char * name,
/*@null@*/ /*@only@*/ char * value,
/*@out@*/ /*@null@*/
struct SH_Status * status)
/*@modifies fragment->attrs@*/
/*@modifies fragment->attr_s@*/
/*@modifies fragment->attr_n@*/
/*@globals fileSystem@*/
/*@modifies fileSystem@*/
/*@modifies status@*/
{
return insert_attr_raw_new (fragment, name, value, 0, status);
}
bool
SH_NodeFragment_prepend_attr_copy (struct SH_NodeFragment * fragment,
const SH_Attr * attr,
......@@ -877,8 +986,8 @@ SH_NodeFragment_append_attr (struct SH_NodeFragment * fragment,
bool
SH_NodeFragment_append_attr_new (struct SH_NodeFragment * fragment,
/*@only@*/ char * name,
/*@null@*/ /*@only@*/ char * value,
const char * name,
/*@null@*/ const char * value,
/*@out@*/ /*@null@*/
struct SH_Status * status)
/*@modifies fragment->attrs@*/
......@@ -892,6 +1001,23 @@ SH_NodeFragment_append_attr_new (struct SH_NodeFragment * fragment,
fragment->attr_n, status);
}
bool
SH_NodeFragment_append_attr_raw_new (struct SH_NodeFragment * fragment,
/*@only@*/ char * name,
/*@null@*/ /*@only@*/ char * value,
/*@out@*/ /*@null@*/
struct SH_Status * status)
/*@modifies fragment->attrs@*/
/*@modifies fragment->attr_s@*/
/*@modifies fragment->attr_n@*/
/*@globals fileSystem@*/
/*@modifies fileSystem@*/
/*@modifies status@*/
{
return insert_attr_raw_new (fragment, name, value,
fragment->attr_n, status);
}
bool
SH_NodeFragment_append_attr_copy (struct SH_NodeFragment * fragment,
const SH_Attr * attr,
......
......@@ -115,8 +115,8 @@ SH_NodeFragment_insert_attr (SH_NodeFragment * fragment,
bool
SH_NodeFragment_insert_attr_new (SH_NodeFragment * fragment,
/*@only@*/ char * name,
/*@null@*/ /*@only@*/ char * value,
const char * name,
/*@null@*/ const char * value,
size_t position,
/*@out@*/ /*@null@*/
struct SH_Status * status)
......@@ -125,6 +125,18 @@ SH_NodeFragment_insert_attr_new (SH_NodeFragment * fragment,
/*@modifies fileSystem@*/
/*@modifies status@*/;
bool
SH_NodeFragment_insert_attr_raw_new (SH_NodeFragment * fragment,
/*@only@*/ char * name,
/*@null@*/ /*@only@*/ char * value,
size_t position,
/*@out@*/ /*@null@*/
struct SH_Status * status)
/*@modifies fragment@*/
/*@globals fileSystem@*/
/*@modifies fileSystem@*/
/*@modifies status@*/;
bool
SH_NodeFragment_insert_attr_copy (SH_NodeFragment * fragment,
const SH_Attr * attr,
......@@ -148,8 +160,8 @@ SH_NodeFragment_prepend_attr (SH_NodeFragment * fragment,
bool
SH_NodeFragment_prepend_attr_new (SH_NodeFragment * fragment,
/*@only@*/ char * name,
/*@null@*/ /*@only@*/ char * value,
const char * name,
/*@null@*/ const char * value,
/*@out@*/ /*@null@*/
struct SH_Status * status)
/*@modifies fragment@*/
......@@ -157,6 +169,17 @@ SH_NodeFragment_prepend_attr_new (SH_NodeFragment * fragment,
/*@modifies fileSystem@*/
/*@modifies status@*/;
bool
SH_NodeFragment_prepend_attr_raw_new (SH_NodeFragment * fragment,
/*@only@*/ char * name,
/*@null@*/ /*@only@*/ char * value,
/*@out@*/ /*@null@*/
struct SH_Status * status)
/*@modifies fragment@*/
/*@globals fileSystem@*/
/*@modifies fileSystem@*/
/*@modifies status@*/;
bool
SH_NodeFragment_prepend_attr_copy (SH_NodeFragment * fragment,
const SH_Attr * attr,
......@@ -179,14 +202,25 @@ SH_NodeFragment_append_attr (SH_NodeFragment * fragment,
bool
SH_NodeFragment_append_attr_new (SH_NodeFragment * fragment,
/*@only@*/ char * name,
/*@null@*/ /*@only@*/ char * value,
const char * name,
/*@null@*/ const char * value,
/*@out@*/ /*@null@*/
struct SH_Status * status)
/*@modifies fragment@*/
/*@globals fileSystem@*/
/*@modifies fileSystem@*/
/*@modifies status@*/;
bool
SH_NodeFragment_append_attr_raw_new (SH_NodeFragment * fragment,
/*@only@*/ char * name,
/*@null@*/ /*@only@*/ char * value,
/*@out@*/ /*@null@*/
struct SH_Status * status)
/*@modifies fragment@*/
/*@globals fileSystem@*/
/*@modifies fileSystem@*/
/*@modifies status@*/;
bool
SH_NodeFragment_append_attr_copy (SH_NodeFragment * fragment,
const SH_Attr * attr,
......
This diff is collapsed.
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