summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul J. Davis <paul.joseph.davis@gmail.com>2019-07-12 10:05:41 -0500
committerPaul J. Davis <paul.joseph.davis@gmail.com>2019-07-12 10:05:41 -0500
commit33046b6cd38d550aab8780c9356105ee1891f98a (patch)
tree9275c8543875f803a476f916ef2f3005eef4ef12
parent015fe58c71555553e913cc40cdb9713aefe5fea3 (diff)
downloadcouchdb-2067-add-get-sort-key.tar.gz
Fix segfault for sort keys larger than 64 bytes2067-add-get-sort-key
The docs say we just have to set PORT_CONTROL_FLAG_BINARY to get the binary return value to work correctly.
-rw-r--r--src/couch/priv/icu_driver/couch_icu_driver.c19
-rw-r--r--src/couch/src/couch_util.erl2
2 files changed, 13 insertions, 8 deletions
diff --git a/src/couch/priv/icu_driver/couch_icu_driver.c b/src/couch/priv/icu_driver/couch_icu_driver.c
index cfa768223..b7eded9e4 100644
--- a/src/couch/priv/icu_driver/couch_icu_driver.c
+++ b/src/couch/priv/icu_driver/couch_icu_driver.c
@@ -56,6 +56,8 @@ static ErlDrvData couch_drv_start(ErlDrvPort port, char *buff)
UErrorCode status = U_ZERO_ERROR;
couch_drv_data* pData = (couch_drv_data*)driver_alloc(sizeof(couch_drv_data));
+ set_port_control_flags(port, PORT_CONTROL_FLAG_BINARY);
+
if (pData == NULL)
return ERL_DRV_ERROR_GENERAL;
@@ -86,14 +88,17 @@ ErlDrvSSizeT
return_control_result(void* pLocalResult, int localLen,
char **ppRetBuf, ErlDrvSizeT returnLen)
{
+ ErlDrvBinary* buf = NULL;
+
if (*ppRetBuf == NULL || localLen > returnLen) {
- *ppRetBuf = (char*)driver_alloc_binary(localLen);
- if(*ppRetBuf == NULL) {
- return -1;
- }
- }
- memcpy(*ppRetBuf, pLocalResult, localLen);
- return localLen;
+ buf = driver_alloc_binary(localLen);
+ memcpy(buf->orig_bytes, pLocalResult, localLen);
+ *ppRetBuf = (char*) buf;
+ return localLen;
+ } else {
+ memcpy(*ppRetBuf, pLocalResult, localLen);
+ return localLen;
+ }
}
static ErlDrvSSizeT
diff --git a/src/couch/src/couch_util.erl b/src/couch/src/couch_util.erl
index adcc3e8d7..dddc71815 100644
--- a/src/couch/src/couch_util.erl
+++ b/src/couch/src/couch_util.erl
@@ -406,7 +406,7 @@ collate(A, B, Options) when is_binary(A), is_binary(B) ->
SizeA = byte_size(A),
SizeB = byte_size(B),
Bin = <<SizeA:32/native, A/binary, SizeB:32/native, B/binary>>,
- [Result] = erlang:port_control(drv_port(), Operation, Bin),
+ <<Result>> = erlang:port_control(drv_port(), Operation, Bin),
% Result is 0 for lt, 1 for eq and 2 for gt. Subtract 1 to return the
% expected typical -1, 0, 1
Result - 1.