aboutsummaryrefslogtreecommitdiffstats
path: root/lib/erl_interface/src
diff options
context:
space:
mode:
authorLukas Larsson <[email protected]>2013-01-28 10:27:26 +0100
committerLukas Larsson <[email protected]>2013-01-28 10:27:26 +0100
commitb5c654bd8f072754a6b7823c53c3106c026871f7 (patch)
tree1f551c43cea902ab727a4bde001bf8b356d9a5ac /lib/erl_interface/src
parentcab17a9f36d2343e01f6aef64416d2a42308c25b (diff)
parent6058758c7b81cf458b1498bef76d672a207df4e8 (diff)
downloadotp-b5c654bd8f072754a6b7823c53c3106c026871f7.tar.gz
otp-b5c654bd8f072754a6b7823c53c3106c026871f7.tar.bz2
otp-b5c654bd8f072754a6b7823c53c3106c026871f7.zip
Merge branch 'lukas/erl_interface/test_atom_as_functions/OTP-10753'
* lukas/erl_interface/test_atom_as_functions/OTP-10753: Return if size test fails Add utf8 atom encode and decode testcases Validate UTF8/ASCII when buf is NULL
Diffstat (limited to 'lib/erl_interface/src')
-rw-r--r--lib/erl_interface/src/encode/encode_atom.c33
-rw-r--r--lib/erl_interface/src/misc/ei_x_encode.c51
2 files changed, 51 insertions, 33 deletions
diff --git a/lib/erl_interface/src/encode/encode_atom.c b/lib/erl_interface/src/encode/encode_atom.c
index 4a2147073f..32f5ae2af1 100644
--- a/lib/erl_interface/src/encode/encode_atom.c
+++ b/lib/erl_interface/src/encode/encode_atom.c
@@ -23,8 +23,8 @@
#include "putget.h"
-static int copy_ascii_atom(char* dst, const char* src, int slen);
-static int copy_utf8_atom(char* dst, const char* src, int slen);
+static int verify_ascii_atom(const char* src, int slen);
+static int verify_utf8_atom(const char* src, int slen);
int ei_encode_atom(char *buf, int *index, const char *p)
@@ -73,7 +73,8 @@ int ei_encode_atom_len_as(char *buf, int *index, const char *p, int len,
if (len < 0) return -1;
break;
case ERLANG_ASCII:
- if (copy_ascii_atom(s+2, p, len) < 0) return -1;
+ if (verify_ascii_atom(p, len) < 0) return -1;
+ memcpy(s+2, p, len);
break;
case ERLANG_LATIN1:
memcpy(s+2, p, len);
@@ -88,7 +89,8 @@ int ei_encode_atom_len_as(char *buf, int *index, const char *p, int len,
if (from_enc == ERLANG_UTF8) {
len = utf8_to_latin1(NULL, p, len, MAXATOMLEN-1, NULL);
if (len < 0) return -1;
- }
+ } else if (from_enc == ERLANG_ASCII)
+ if (verify_ascii_atom(p, len) < 0) return -1;
}
break;
@@ -100,11 +102,13 @@ int ei_encode_atom_len_as(char *buf, int *index, const char *p, int len,
len = latin1_to_utf8((buf ? s+offs : NULL), p, len, MAXATOMLEN_UTF8-1, NULL);
break;
case ERLANG_ASCII:
- if (buf && copy_ascii_atom(s+offs, p, len) < 0) return -1;
+ if (verify_ascii_atom(p, len) < 0) return -1;
+ if (buf) memcpy(s+offs,p,len);
break;
case ERLANG_UTF8:
if (len >= 256) offs++;
- if (buf && copy_utf8_atom(s+offs, p, len) < 0) return -1;
+ if (verify_utf8_atom(p, len) < 0) return -1;
+ if (buf) memcpy(s+offs,p,len);
break;
default:
return -1;
@@ -144,17 +148,17 @@ ei_internal_put_atom(char** bufp, const char* p, int slen,
}
-int copy_ascii_atom(char* dst, const char* src, int slen)
+int verify_ascii_atom(const char* src, int slen)
{
while (slen > 0) {
if ((src[0] & 0x80) != 0) return -1;
- *dst++ = *src++;
+ src++;
slen--;
}
return 0;
}
-int copy_utf8_atom(char* dst, const char* src, int slen)
+int verify_utf8_atom(const char* src, int slen)
{
int num_chars = 0;
@@ -163,25 +167,22 @@ int copy_utf8_atom(char* dst, const char* src, int slen)
if ((src[0] & 0x80) != 0) {
if ((src[0] & 0xE0) == 0xC0) {
if (slen < 2 || (src[1] & 0xC0) != 0x80) return -1;
- *dst++ = *src++;
+ src++;
slen--;
}
else if ((src[0] & 0xF0) == 0xE0) {
if (slen < 3 || (src[1] & 0xC0) != 0x80 || (src[2] & 0xC0) != 0x80) return -1;
- *dst++ = *src++;
- *dst++ = *src++;
+ src += 2;
slen -= 2;
}
else if ((src[0] & 0xF8) == 0xF0) {
if (slen < 4 || (src[1] & 0xC0) != 0x80 || (src[2] & 0xC0) != 0x80 || (src[3] & 0xC0) != 0x80) return -1;
- *dst++ = *src++;
- *dst++ = *src++;
- *dst++ = *src++;
+ src += 3;
slen -= 3;
}
else return -1;
}
- *dst++ = *src++;
+ src++;
slen--;
}
return 0;
diff --git a/lib/erl_interface/src/misc/ei_x_encode.c b/lib/erl_interface/src/misc/ei_x_encode.c
index ec84ca4bbe..4e6f4a1d36 100644
--- a/lib/erl_interface/src/misc/ei_x_encode.c
+++ b/lib/erl_interface/src/misc/ei_x_encode.c
@@ -99,7 +99,8 @@ int ei_x_encode_string(ei_x_buff* x, const char* s)
int ei_x_encode_string_len(ei_x_buff* x, const char* s, int len)
{
int i = x->index;
- ei_encode_string_len(NULL, &i, s, len);
+ if (ei_encode_string_len(NULL, &i, s, len) == -1)
+ return -1;
if (!x_fix_buff(x, i))
return -1;
return ei_encode_string_len(x->buff, &x->index, s, len);
@@ -108,7 +109,8 @@ int ei_x_encode_string_len(ei_x_buff* x, const char* s, int len)
int ei_x_encode_binary(ei_x_buff* x, const void* p, int len)
{
int i = x->index;
- ei_encode_binary(NULL, &i, p, len);
+ if (ei_encode_binary(NULL, &i, p, len) == -1)
+ return -1;
if (!x_fix_buff(x, i))
return -1;
return ei_encode_binary(x->buff, &x->index, p, len);
@@ -117,7 +119,8 @@ int ei_x_encode_binary(ei_x_buff* x, const void* p, int len)
int ei_x_encode_long(ei_x_buff* x, long n)
{
int i = x->index;
- ei_encode_long(NULL, &i, n);
+ if (ei_encode_long(NULL, &i, n) == -1)
+ return -1;
if (!x_fix_buff(x, i))
return -1;
return ei_encode_long(x->buff, &x->index, n);
@@ -126,7 +129,8 @@ int ei_x_encode_long(ei_x_buff* x, long n)
int ei_x_encode_ulong(ei_x_buff* x, unsigned long n)
{
int i = x->index;
- ei_encode_ulong(NULL, &i, n);
+ if (ei_encode_ulong(NULL, &i, n) == -1)
+ return -1;
if (!x_fix_buff(x, i))
return -1;
return ei_encode_ulong(x->buff, &x->index, n);
@@ -135,7 +139,8 @@ int ei_x_encode_ulong(ei_x_buff* x, unsigned long n)
int ei_x_encode_char(ei_x_buff* x, char p)
{
int i = x->index;
- ei_encode_char(NULL, &i, p);
+ if (ei_encode_char(NULL, &i, p) == -1)
+ return -1;
if (!x_fix_buff(x, i))
return -1;
return ei_encode_char(x->buff, &x->index, p);
@@ -144,7 +149,8 @@ int ei_x_encode_char(ei_x_buff* x, char p)
int ei_x_encode_boolean(ei_x_buff* x, int p)
{
int i = x->index;
- ei_encode_boolean(NULL, &i, p);
+ if (ei_encode_boolean(NULL, &i, p) == -1)
+ return -1;
if (!x_fix_buff(x, i))
return -1;
return ei_encode_boolean(x->buff, &x->index, p);
@@ -153,7 +159,8 @@ int ei_x_encode_boolean(ei_x_buff* x, int p)
int ei_x_encode_double(ei_x_buff* x, double dbl)
{
int i = x->index;
- ei_encode_double(NULL, &i, dbl);
+ if (ei_encode_double(NULL, &i, dbl) == -1)
+ return -1;
if (!x_fix_buff(x, i))
return -1;
return ei_encode_double(x->buff, &x->index, dbl);
@@ -162,7 +169,8 @@ int ei_x_encode_double(ei_x_buff* x, double dbl)
int ei_x_encode_list_header(ei_x_buff* x, long n)
{
int i = x->index;
- ei_encode_list_header(NULL, &i, n);
+ if (ei_encode_list_header(NULL, &i, n) == -1)
+ return -1;
if (!x_fix_buff(x, i))
return -1;
return ei_encode_list_header(x->buff, &x->index, n);
@@ -171,7 +179,8 @@ int ei_x_encode_list_header(ei_x_buff* x, long n)
int ei_x_encode_empty_list(ei_x_buff* x)
{
int i = x->index;
- ei_encode_empty_list(NULL, &i);
+ if (ei_encode_empty_list(NULL, &i) == -1)
+ return -1;
if (!x_fix_buff(x, i))
return -1;
return ei_encode_empty_list(x->buff, &x->index);
@@ -180,7 +189,8 @@ int ei_x_encode_empty_list(ei_x_buff* x)
int ei_x_encode_version(ei_x_buff* x)
{
int i = x->index;
- ei_encode_version(NULL, &i);
+ if (ei_encode_version(NULL, &i) == -1)
+ return -1;
if (!x_fix_buff(x, i))
return -1;
return ei_encode_version(x->buff, &x->index);
@@ -189,7 +199,8 @@ int ei_x_encode_version(ei_x_buff* x)
int ei_x_encode_tuple_header(ei_x_buff* x, long n)
{
int i = x->index;
- ei_encode_tuple_header(NULL, &i, n);
+ if (ei_encode_tuple_header(NULL, &i, n) == -1)
+ return -1;
if (!x_fix_buff(x, i))
return -1;
return ei_encode_tuple_header(x->buff, &x->index, n);
@@ -217,7 +228,8 @@ int ei_x_encode_atom_len_as(ei_x_buff* x, const char* s, int len,
enum erlang_char_encoding to_enc)
{
int i = x->index;
- ei_encode_atom_len_as(NULL, &i, s, len, from_enc, to_enc);
+ if (ei_encode_atom_len_as(NULL, &i, s, len, from_enc, to_enc) == -1)
+ return -1;
if (!x_fix_buff(x, i))
return -1;
return ei_encode_atom_len_as(x->buff, &x->index, s, len, from_enc, to_enc);
@@ -227,7 +239,8 @@ int ei_x_encode_atom_len_as(ei_x_buff* x, const char* s, int len,
int ei_x_encode_pid(ei_x_buff* x, const erlang_pid* pid)
{
int i = x->index;
- ei_encode_pid(NULL, &i, pid);
+ if (ei_encode_pid(NULL, &i, pid) == -1)
+ return -1;
if (!x_fix_buff(x, i))
return -1;
return ei_encode_pid(x->buff, &x->index, pid);
@@ -236,7 +249,8 @@ int ei_x_encode_pid(ei_x_buff* x, const erlang_pid* pid)
int ei_x_encode_fun(ei_x_buff* x, const erlang_fun* fun)
{
int i = x->index;
- ei_encode_fun(NULL, &i, fun);
+ if (ei_encode_fun(NULL, &i, fun) == -1)
+ return -1;
if (!x_fix_buff(x, i))
return -1;
return ei_encode_fun(x->buff, &x->index, fun);
@@ -245,7 +259,8 @@ int ei_x_encode_fun(ei_x_buff* x, const erlang_fun* fun)
int ei_x_encode_ref(ei_x_buff* x, const erlang_ref* ref)
{
int i = x->index;
- ei_encode_ref(NULL, &i, ref);
+ if (ei_encode_ref(NULL, &i, ref) == -1)
+ return -1;
if (!x_fix_buff(x, i))
return -1;
return ei_encode_ref(x->buff, &x->index, ref);
@@ -254,7 +269,8 @@ int ei_x_encode_ref(ei_x_buff* x, const erlang_ref* ref)
int ei_x_encode_port(ei_x_buff* x, const erlang_port* port)
{
int i = x->index;
- ei_encode_port(NULL, &i, port);
+ if (ei_encode_port(NULL, &i, port) == -1)
+ return -1;
if (!x_fix_buff(x, i))
return -1;
return ei_encode_port(x->buff, &x->index, port);
@@ -263,7 +279,8 @@ int ei_x_encode_port(ei_x_buff* x, const erlang_port* port)
int ei_x_encode_trace(ei_x_buff* x, const erlang_trace* trace)
{
int i = x->index;
- ei_encode_trace(NULL, &i, trace);
+ if (ei_encode_trace(NULL, &i, trace) == -1)
+ return -1;
if (!x_fix_buff(x, i))
return -1;
return ei_encode_trace(x->buff, &x->index, trace);