BUGFIX reflect default argument promotions in va_* function
char and short variadic arguments automatically promotes to int. This
must be reflected in va_arg() and the int type is supposed to be used
as type to obtain instead of the original type.
Furthermore, the last non-variadic argument cannot be regiter variable,
function, array or automatically promoted type.
In same case, we are using enum type as the last non-variadic and variadic
arguments and above restrictins were not reflected. Even that the
enum is represented as int on common architectures / by common compilers,
the restrictions should be reflected.
Fixes #12
diff --git a/src/io.c b/src/io.c
index 979e84b..ca238e3 100644
--- a/src/io.c
+++ b/src/io.c
@@ -958,7 +958,7 @@
/* return -1 can change session status */
int
-nc_write_msg(struct nc_session *session, NC_MSG_TYPE type, ...)
+nc_write_msg(struct nc_session *session, int type, ...)
{
va_list ap;
int count;
@@ -986,6 +986,7 @@
arg.session = session;
arg.len = 0;
+
switch (type) {
case NC_MSG_RPC:
content = va_arg(ap, struct lyd_node *);
diff --git a/src/messages_server.c b/src/messages_server.c
index 1d7aec4..918319c 100644
--- a/src/messages_server.c
+++ b/src/messages_server.c
@@ -123,7 +123,7 @@
}
API struct nc_server_error *
-nc_err(NC_ERR tag, ...)
+nc_err(int tag, ...)
{
va_list ap;
struct nc_server_error *ret;
@@ -151,7 +151,7 @@
case NC_ERR_ACCESS_DENIED:
case NC_ERR_ROLLBACK_FAILED:
case NC_ERR_OP_NOT_SUPPORTED:
- type = va_arg(ap, NC_ERR_TYPE);
+ type = (NC_ERR_TYPE)va_arg(ap, int); /* NC_ERR_TYPE enum is automatically promoted to int */
if ((type != NC_ERR_TYPE_PROT) && (type != NC_ERR_TYPE_APP)) {
ERRARG("type");
goto fail;
@@ -160,14 +160,14 @@
case NC_ERR_TOO_BIG:
case NC_ERR_RES_DENIED:
- type = va_arg(ap, NC_ERR_TYPE);
+ type = (NC_ERR_TYPE)va_arg(ap, int); /* NC_ERR_TYPE enum is automatically promoted to int */
/* nothing to check */
break;
case NC_ERR_MISSING_ATTR:
case NC_ERR_BAD_ATTR:
case NC_ERR_UNKNOWN_ATTR:
- type = va_arg(ap, NC_ERR_TYPE);
+ type = (NC_ERR_TYPE)va_arg(ap, int); /* NC_ERR_TYPE enum is automatically promoted to int */
arg1 = va_arg(ap, const char *);
arg2 = va_arg(ap, const char *);
@@ -182,7 +182,7 @@
case NC_ERR_MISSING_ELEM:
case NC_ERR_BAD_ELEM:
case NC_ERR_UNKNOWN_ELEM:
- type = va_arg(ap, NC_ERR_TYPE);
+ type = (NC_ERR_TYPE)va_arg(ap, int); /* NC_ERR_TYPE enum is automatically promoted to int */
arg1 = va_arg(ap, const char *);
if ((type != NC_ERR_TYPE_PROT) && (type != NC_ERR_TYPE_APP)) {
@@ -193,7 +193,7 @@
break;
case NC_ERR_UNKNOWN_NS:
- type = va_arg(ap, NC_ERR_TYPE);
+ type = (NC_ERR_TYPE)va_arg(ap, int); /* NC_ERR_TYPE enum is automatically promoted to int */
arg1 = va_arg(ap, const char *);
arg2 = va_arg(ap, const char *);
@@ -218,7 +218,7 @@
break;
case NC_ERR_OP_FAILED:
- type = va_arg(ap, NC_ERR_TYPE);
+ type = (NC_ERR_TYPE)va_arg(ap, int); /* NC_ERR_TYPE enum is automatically promoted to int */
if (type == NC_ERR_TYPE_TRAN) {
ERRARG("type");
diff --git a/src/messages_server.h b/src/messages_server.h
index feb5bdc..c615786 100644
--- a/src/messages_server.h
+++ b/src/messages_server.h
@@ -111,7 +111,7 @@
* @brief Create a server error structure. Its \<error-message\> is filled with
* a general description of the specific error.
*
- * @param[in] tag \<error-tag\> of the server error. According to the tag, the
+ * @param[in] tag \<error-tag\> of the server error specified as #NC_ERR value. According to the tag, the
* specific additional parameters are required:
* - #NC_ERR_IN_USE
* - #NC_ERR_INVALID_VALUE
@@ -145,7 +145,7 @@
* - no additional arguments
* @return Server error structure, NULL on error.
*/
-struct nc_server_error *nc_err(NC_ERR tag, ...);
+struct nc_server_error *nc_err(int tag, ...);
/**
* @brief Create a server error structure based on libyang error.
diff --git a/src/session_p.h b/src/session_p.h
index ec4508f..e558a44 100644
--- a/src/session_p.h
+++ b/src/session_p.h
@@ -564,7 +564,7 @@
* @brief Write message into wire.
*
* @param[in] session NETCONF session to which the message will be written.
- * @param[in] type Type of the message to write. According to the type, the
+ * @param[in] type The type of the message to write, specified as #NC_MSG_TYPE value. According to the type, the
* specific additional parameters are required or accepted:
* - #NC_MSG_RPC
* - `struct lyd_node *op;` - operation (content of the \<rpc/\> to be sent. Required parameter.
@@ -579,7 +579,7 @@
* - TODO: content
* @return 0 on success
*/
-int nc_write_msg(struct nc_session *session, NC_MSG_TYPE type, ...);
+int nc_write_msg(struct nc_session *session, int type, ...);
/**
* @brief Check whether a session is still connected (on transport layer).