2020-08-06 12:41:58 -07:00
|
|
|
/* SDSLib 2.0 -- A C dynamic strings library
|
|
|
|
*
|
2024-08-14 17:20:36 +01:00
|
|
|
* Copyright (c) 2006-2015, Redis Ltd.
|
2020-08-06 12:41:58 -07:00
|
|
|
* Copyright (c) 2015, Oran Agra
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
* to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fmacros.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include "sds.h"
|
|
|
|
#include "sdsalloc.h"
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
static inline int hi_sdsHdrSize(char type) {
|
|
|
|
switch(type&HI_SDS_TYPE_MASK) {
|
|
|
|
case HI_SDS_TYPE_5:
|
|
|
|
return sizeof(struct hisdshdr5);
|
|
|
|
case HI_SDS_TYPE_8:
|
|
|
|
return sizeof(struct hisdshdr8);
|
|
|
|
case HI_SDS_TYPE_16:
|
|
|
|
return sizeof(struct hisdshdr16);
|
|
|
|
case HI_SDS_TYPE_32:
|
|
|
|
return sizeof(struct hisdshdr32);
|
|
|
|
case HI_SDS_TYPE_64:
|
|
|
|
return sizeof(struct hisdshdr64);
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
static inline char hi_sdsReqType(size_t string_size) {
|
2020-08-06 12:41:58 -07:00
|
|
|
if (string_size < 32)
|
2020-08-15 12:24:31 -07:00
|
|
|
return HI_SDS_TYPE_5;
|
2020-08-06 12:41:58 -07:00
|
|
|
if (string_size < 0xff)
|
2020-08-15 12:24:31 -07:00
|
|
|
return HI_SDS_TYPE_8;
|
2020-08-06 12:41:58 -07:00
|
|
|
if (string_size < 0xffff)
|
2020-08-15 12:24:31 -07:00
|
|
|
return HI_SDS_TYPE_16;
|
2020-08-06 12:41:58 -07:00
|
|
|
if (string_size < 0xffffffff)
|
2020-08-15 12:24:31 -07:00
|
|
|
return HI_SDS_TYPE_32;
|
|
|
|
return HI_SDS_TYPE_64;
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Create a new hisds string with the content specified by the 'init' pointer
|
2020-08-06 12:41:58 -07:00
|
|
|
* and 'initlen'.
|
|
|
|
* If NULL is used for 'init' the string is initialized with zero bytes.
|
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* The string is always null-terminated (all the hisds strings are, always) so
|
|
|
|
* even if you create an hisds string with:
|
2020-08-06 12:41:58 -07:00
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* mystring = hi_sdsnewlen("abc",3);
|
2020-08-06 12:41:58 -07:00
|
|
|
*
|
|
|
|
* You can print the string with printf() as there is an implicit \0 at the
|
|
|
|
* end of the string. However the string is binary safe and can contain
|
2020-08-15 12:24:31 -07:00
|
|
|
* \0 characters in the middle, as the length is stored in the hisds header. */
|
|
|
|
hisds hi_sdsnewlen(const void *init, size_t initlen) {
|
2020-08-06 12:41:58 -07:00
|
|
|
void *sh;
|
2020-08-15 12:24:31 -07:00
|
|
|
hisds s;
|
|
|
|
char type = hi_sdsReqType(initlen);
|
2020-08-06 12:41:58 -07:00
|
|
|
/* Empty strings are usually created in order to append. Use type 8
|
|
|
|
* since type 5 is not good at this. */
|
2020-08-15 12:24:31 -07:00
|
|
|
if (type == HI_SDS_TYPE_5 && initlen == 0) type = HI_SDS_TYPE_8;
|
|
|
|
int hdrlen = hi_sdsHdrSize(type);
|
2020-08-06 12:41:58 -07:00
|
|
|
unsigned char *fp; /* flags pointer. */
|
|
|
|
|
Squashed 'deps/hiredis/' changes from f8de9a4bd..b6a052fe0
b6a052fe0 Helper for setting TCP_USER_TIMEOUT socket option (#1188)
3fa9b6944 Add RedisModule adapter (#1182)
d13c091e9 Fix wincrypt symbols conflict
5d84c8cfd Add a test ensuring we don't clobber connection error.
3f95fcdae Don't attempt to set a timeout if we are in an error state.
aacb84b8d Fix typo in makefile.
563b062e3 Accept -nan per the RESP3 spec recommendation.
04c1b5b02 Fix colliding option values
4ca8e73f6 Rework searching for openssl
cd208812f Attempt to find the correct path for openssl.
011f7093c Allow specifying the keepalive interval
e9243d4f7 Cmake static or shared (#1160)
1cbd5bc76 Write a version file for the CMake package (#1165)
6f5bae8c6 fix typo
acd09461d CMakeLists.txt: respect BUILD_SHARED_LIBS
97fcf0fd1 Add sdevent adapter
ccff093bc Bump dev version for the next release cycle.
c14775b4e Prepare for v1.1.0 GA
f0bdf8405 Add support for nan in RESP3 double (#1133)
991b0b0b3 Add an example that calls redisCommandArgv (#1140)
a36686f84 CI updates (#1139)
8ad4985e9 fix flag reference
7583ebb1b Make freeing a NULL redisAsyncContext a no op.
2c53dea7f Update version in dev branch.
f063370ed Prepare for v1.1.0-rc1
2b069573a CI fixes in preparation of release
e1e9eb40d Add author information to release-drafter template.
afc29ee1a Update for mingw cross compile
ceb8a8815 fixed cpp build error with adapters/libhv.h
3b15a04b5 Fixup of PR734: Coverage of hiredis.c (#1124)
c245df9fb CMake corrections for building on Windows (#1122)
9c338a598 Fix PUSH handler tests for Redis >= 7.0.5
6d5c3ee74 Install on windows fixes (#1117)
68b29e1ad Add timeout support to libhv adapter. (#1109)
722e3409c Additional include directory given by pkg-config (#1118)
bd9ccb8c4 Use __attribute__ when building with clang on windows
5392adc26 set default SSL certificate directory
560e66486 Minor refactor
d756f68a5 Add libhv example to our standard Makefile
a66916719 Add adapters/libhv
855b48a81 Fix pkgconfig for hiredis_ssl
79ae5ffc6 Fix protocol error (#1106)
61b5b299f Use a windows specific keepalive function. (#1104)
fce8abc1c Introduce .close method for redisContextFuncs
cfb6ca881 Add REDIS_OPT_PREFER_UNSPEC (#1101)
cc7c35ce6 Update documentation to explain redisConnectWithOptions.
bc8d837b7 fix heap-buffer-overflow (#957)
ca4a0e850 uvadapter: reduce number of uv_poll_start calls
35d398c90 Fix cmake config path on Linux. CMake config files were installed to `/usr/local/share/hiredis`, which is not recognizable by `find_package()`. I'm not sure why it was set that way. Given the commit introducing it is for Windows, I keep that behavior consistent there, but fix the rest.
10c78c6e1 Add possibility to prefer IPv6, IPv4 or unspecified
1abe0c828 fuzzer: No alloc in redisFormatCommand() when fail
329eaf9ba Fix heap-buffer-overflow issue in redisvFormatCommad
eaae7321c Polling adapter requires sockcompat.h
0a5fa3dde Regression test for off-by-one parsing error
9e174e8f7 Add do while(0) protection for macros
4ad99c69a Rework asSleep to be a generic millisleep function.
75cb6c1ea Do store command timeout in the context for redisSetTimeout (#593)
c57cad658 CMake: remove dict.c form hiredis_sources
8491a65a9 Add Github Actions CI workflow for hiredis: Arm, Arm64, 386, windows. (#943)
77e4f09ea Merge pull request #964 from afcidk/fix-createDoubleObject
9219f7e7c Merge pull request #901 from devnexen/illumos_test_fix
810cc6104 Merge pull request #905 from sundb/master
df8b74d69 Merge pull request #1091 from redis/ssl-error-ub-fix
0ed6cdec3 Fix some undefined behaviour
507a6dcaa Merge pull request #1090 from Nordix/subscribe-oom-error
b044eaa6a Copy error to redisAsyncContext when finding subscribe cb
e0200b797 Merge pull request #1087 from redis/const-and-non-const-callback
6a3e96ad2 Maintain backward compatibiliy withour onConnect callback.
e7afd998f Merge pull request #1079 from SukkaW/drop-macos-10.15-runner
17c8fe079 Merge pull request #931 from kristjanvalur/pr2
b808c0c20 Merge pull request #1083 from chayim/ck-drafter
367a82bf0 Merge pull request #1085 from stanhu/ssl-improve-options-setting
71119a71d Make it possible to set SSL verify mode
dd7979ac1 Merge pull request #1084 from stanhu/sh-improve-ssl-docs
c71116178 Improve example for SSL initialization in README.md
5c9b6b571 Release drafter
a606ccf2a CI: use recommended `vmactions/freebsd-vm@v0`
0865c115b Merge pull request #1080 from Nordix/readme-corrections
f6cee7142 Fix README typos
06be7ff31 Merge pull request #1050 from smmir-cent/fix-cmake-version
7dd833d54 CI: bump macos runner version
f69fac769 Drop `const` on redisAsyncContext in redisConnectCallback Since the callback is now re-entrant, it can call apis such as redisAsyncDisconnect()
005d7edeb Support calling redisAsyncDisconnect from the onConnected callback, by deferring context deletion
6ed060920 Add async regression test for issue #931
eaa2a7ee7 Merge pull request #932 from kristjanvalur/pr3
2ccef30f3 Add regression test for issue #945
4b901d44a Initial async tests
31c91408e Polling adapter and example
8a15f4d65 Merge pull request #1057 from orgads/static-name
902dd047f Merge pull request #1054 from kristjanvalur/pr08
c78d0926b Merge pull request #1074 from michael-grunder/kristjanvalur-pr4
2b115d56c Whitespace
1343988ce Fix typos
47b57aa24 Add some documentation on connect/disconnect callbacks and command callbacks
a890d9ce2 Merge pull request #1073 from michael-grunder/kristjanvalur-pr1
f246ee433 Whitespace, style
94c1985bd Use correct type for getsockopt()
5e002bc21 Support failed async connects on windows.
5d68ad2f4 Merge pull request #1072 from michael-grunder/fix-redis7-unit-tests
f4b6ed289 Fix tests so they work for Redis 7.0
95a0c1283 Merge pull request #1058 from orgads/win64
eedb37a65 Fix warnings on Win64
47c3ecefc Merge pull request #1062 from yossigo/fix-push-notification-order
e23d91c97 Merge pull request #1061 from yossigo/update-redis-apt
34211ad54 Merge pull request #1063 from redis/fix-windows-tests
9957af7e3 Whitelist hiredis repo path in cygwin
b455b3381 Handle push notifications before or after reply.
aed9ce446 Use official repository for redis package.
d7683f35a Merge pull request #1047 from Nordix/unsubscribe-handling
7c44a9d7e Merge pull request #1045 from Nordix/sds-updates
dd4bf9783 Use the same name for static and shared libraries
ff57c18b9 Embed debug information in windows static lib, rather than create a .pdb file
8310ad4f5 fix cmake version
7123b87f6 Handle any pipelined unsubscribe in async
b6fb548fc Ignore pubsub replies without a channel/pattern
00b82683b Handle overflows as errors instead of asserting
64062a1d4 Catch size_t overflows in sds.c
066c6de79 Use size_t/long to avoid truncation
c6657ef65 Merge branch 'redis:master' into master
50cdcab49 Fix potential fault at createDoubleObject
fd033e983 Remove semicolon after do-while in _EL_CLEANUP
664c415e7 Illumos test fixes, error message difference fot bad hostname test.
git-subtree-dir: deps/hiredis
git-subtree-split: b6a052fe0959dae69e16b9d74449faeb1b70dbe1
2023-05-30 22:23:45 +03:00
|
|
|
if (hdrlen+initlen+1 <= initlen) return NULL; /* Catch size_t overflow */
|
2020-08-15 12:24:31 -07:00
|
|
|
sh = hi_s_malloc(hdrlen+initlen+1);
|
2020-08-06 12:41:58 -07:00
|
|
|
if (sh == NULL) return NULL;
|
|
|
|
if (!init)
|
|
|
|
memset(sh, 0, hdrlen+initlen+1);
|
|
|
|
s = (char*)sh+hdrlen;
|
|
|
|
fp = ((unsigned char*)s)-1;
|
|
|
|
switch(type) {
|
2020-08-15 12:24:31 -07:00
|
|
|
case HI_SDS_TYPE_5: {
|
|
|
|
*fp = type | (initlen << HI_SDS_TYPE_BITS);
|
2020-08-06 12:41:58 -07:00
|
|
|
break;
|
|
|
|
}
|
2020-08-15 12:24:31 -07:00
|
|
|
case HI_SDS_TYPE_8: {
|
|
|
|
HI_SDS_HDR_VAR(8,s);
|
2020-08-06 12:41:58 -07:00
|
|
|
sh->len = initlen;
|
|
|
|
sh->alloc = initlen;
|
|
|
|
*fp = type;
|
|
|
|
break;
|
|
|
|
}
|
2020-08-15 12:24:31 -07:00
|
|
|
case HI_SDS_TYPE_16: {
|
|
|
|
HI_SDS_HDR_VAR(16,s);
|
2020-08-06 12:41:58 -07:00
|
|
|
sh->len = initlen;
|
|
|
|
sh->alloc = initlen;
|
|
|
|
*fp = type;
|
|
|
|
break;
|
|
|
|
}
|
2020-08-15 12:24:31 -07:00
|
|
|
case HI_SDS_TYPE_32: {
|
|
|
|
HI_SDS_HDR_VAR(32,s);
|
2020-08-06 12:41:58 -07:00
|
|
|
sh->len = initlen;
|
|
|
|
sh->alloc = initlen;
|
|
|
|
*fp = type;
|
|
|
|
break;
|
|
|
|
}
|
2020-08-15 12:24:31 -07:00
|
|
|
case HI_SDS_TYPE_64: {
|
|
|
|
HI_SDS_HDR_VAR(64,s);
|
2020-08-06 12:41:58 -07:00
|
|
|
sh->len = initlen;
|
|
|
|
sh->alloc = initlen;
|
|
|
|
*fp = type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (initlen && init)
|
|
|
|
memcpy(s, init, initlen);
|
|
|
|
s[initlen] = '\0';
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Create an empty (zero length) hisds string. Even in this case the string
|
2020-08-06 12:41:58 -07:00
|
|
|
* always has an implicit null term. */
|
2020-08-15 12:24:31 -07:00
|
|
|
hisds hi_sdsempty(void) {
|
|
|
|
return hi_sdsnewlen("",0);
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Create a new hisds string starting from a null terminated C string. */
|
|
|
|
hisds hi_sdsnew(const char *init) {
|
2020-08-06 12:41:58 -07:00
|
|
|
size_t initlen = (init == NULL) ? 0 : strlen(init);
|
2020-08-15 12:24:31 -07:00
|
|
|
return hi_sdsnewlen(init, initlen);
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Duplicate an hisds string. */
|
|
|
|
hisds hi_sdsdup(const hisds s) {
|
|
|
|
return hi_sdsnewlen(s, hi_sdslen(s));
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Free an hisds string. No operation is performed if 's' is NULL. */
|
|
|
|
void hi_sdsfree(hisds s) {
|
2020-08-06 12:41:58 -07:00
|
|
|
if (s == NULL) return;
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_s_free((char*)s-hi_sdsHdrSize(s[-1]));
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Set the hisds string length to the length as obtained with strlen(), so
|
2020-08-06 12:41:58 -07:00
|
|
|
* considering as content only up to the first null term character.
|
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* This function is useful when the hisds string is hacked manually in some
|
2020-08-06 12:41:58 -07:00
|
|
|
* way, like in the following example:
|
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* s = hi_sdsnew("foobar");
|
2020-08-06 12:41:58 -07:00
|
|
|
* s[2] = '\0';
|
2020-08-15 12:24:31 -07:00
|
|
|
* hi_sdsupdatelen(s);
|
|
|
|
* printf("%d\n", hi_sdslen(s));
|
2020-08-06 12:41:58 -07:00
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* The output will be "2", but if we comment out the call to hi_sdsupdatelen()
|
2020-08-06 12:41:58 -07:00
|
|
|
* the output will be "6" as the string was modified but the logical length
|
|
|
|
* remains 6 bytes. */
|
2020-08-15 12:24:31 -07:00
|
|
|
void hi_sdsupdatelen(hisds s) {
|
Squashed 'deps/hiredis/' changes from f8de9a4bd..b6a052fe0
b6a052fe0 Helper for setting TCP_USER_TIMEOUT socket option (#1188)
3fa9b6944 Add RedisModule adapter (#1182)
d13c091e9 Fix wincrypt symbols conflict
5d84c8cfd Add a test ensuring we don't clobber connection error.
3f95fcdae Don't attempt to set a timeout if we are in an error state.
aacb84b8d Fix typo in makefile.
563b062e3 Accept -nan per the RESP3 spec recommendation.
04c1b5b02 Fix colliding option values
4ca8e73f6 Rework searching for openssl
cd208812f Attempt to find the correct path for openssl.
011f7093c Allow specifying the keepalive interval
e9243d4f7 Cmake static or shared (#1160)
1cbd5bc76 Write a version file for the CMake package (#1165)
6f5bae8c6 fix typo
acd09461d CMakeLists.txt: respect BUILD_SHARED_LIBS
97fcf0fd1 Add sdevent adapter
ccff093bc Bump dev version for the next release cycle.
c14775b4e Prepare for v1.1.0 GA
f0bdf8405 Add support for nan in RESP3 double (#1133)
991b0b0b3 Add an example that calls redisCommandArgv (#1140)
a36686f84 CI updates (#1139)
8ad4985e9 fix flag reference
7583ebb1b Make freeing a NULL redisAsyncContext a no op.
2c53dea7f Update version in dev branch.
f063370ed Prepare for v1.1.0-rc1
2b069573a CI fixes in preparation of release
e1e9eb40d Add author information to release-drafter template.
afc29ee1a Update for mingw cross compile
ceb8a8815 fixed cpp build error with adapters/libhv.h
3b15a04b5 Fixup of PR734: Coverage of hiredis.c (#1124)
c245df9fb CMake corrections for building on Windows (#1122)
9c338a598 Fix PUSH handler tests for Redis >= 7.0.5
6d5c3ee74 Install on windows fixes (#1117)
68b29e1ad Add timeout support to libhv adapter. (#1109)
722e3409c Additional include directory given by pkg-config (#1118)
bd9ccb8c4 Use __attribute__ when building with clang on windows
5392adc26 set default SSL certificate directory
560e66486 Minor refactor
d756f68a5 Add libhv example to our standard Makefile
a66916719 Add adapters/libhv
855b48a81 Fix pkgconfig for hiredis_ssl
79ae5ffc6 Fix protocol error (#1106)
61b5b299f Use a windows specific keepalive function. (#1104)
fce8abc1c Introduce .close method for redisContextFuncs
cfb6ca881 Add REDIS_OPT_PREFER_UNSPEC (#1101)
cc7c35ce6 Update documentation to explain redisConnectWithOptions.
bc8d837b7 fix heap-buffer-overflow (#957)
ca4a0e850 uvadapter: reduce number of uv_poll_start calls
35d398c90 Fix cmake config path on Linux. CMake config files were installed to `/usr/local/share/hiredis`, which is not recognizable by `find_package()`. I'm not sure why it was set that way. Given the commit introducing it is for Windows, I keep that behavior consistent there, but fix the rest.
10c78c6e1 Add possibility to prefer IPv6, IPv4 or unspecified
1abe0c828 fuzzer: No alloc in redisFormatCommand() when fail
329eaf9ba Fix heap-buffer-overflow issue in redisvFormatCommad
eaae7321c Polling adapter requires sockcompat.h
0a5fa3dde Regression test for off-by-one parsing error
9e174e8f7 Add do while(0) protection for macros
4ad99c69a Rework asSleep to be a generic millisleep function.
75cb6c1ea Do store command timeout in the context for redisSetTimeout (#593)
c57cad658 CMake: remove dict.c form hiredis_sources
8491a65a9 Add Github Actions CI workflow for hiredis: Arm, Arm64, 386, windows. (#943)
77e4f09ea Merge pull request #964 from afcidk/fix-createDoubleObject
9219f7e7c Merge pull request #901 from devnexen/illumos_test_fix
810cc6104 Merge pull request #905 from sundb/master
df8b74d69 Merge pull request #1091 from redis/ssl-error-ub-fix
0ed6cdec3 Fix some undefined behaviour
507a6dcaa Merge pull request #1090 from Nordix/subscribe-oom-error
b044eaa6a Copy error to redisAsyncContext when finding subscribe cb
e0200b797 Merge pull request #1087 from redis/const-and-non-const-callback
6a3e96ad2 Maintain backward compatibiliy withour onConnect callback.
e7afd998f Merge pull request #1079 from SukkaW/drop-macos-10.15-runner
17c8fe079 Merge pull request #931 from kristjanvalur/pr2
b808c0c20 Merge pull request #1083 from chayim/ck-drafter
367a82bf0 Merge pull request #1085 from stanhu/ssl-improve-options-setting
71119a71d Make it possible to set SSL verify mode
dd7979ac1 Merge pull request #1084 from stanhu/sh-improve-ssl-docs
c71116178 Improve example for SSL initialization in README.md
5c9b6b571 Release drafter
a606ccf2a CI: use recommended `vmactions/freebsd-vm@v0`
0865c115b Merge pull request #1080 from Nordix/readme-corrections
f6cee7142 Fix README typos
06be7ff31 Merge pull request #1050 from smmir-cent/fix-cmake-version
7dd833d54 CI: bump macos runner version
f69fac769 Drop `const` on redisAsyncContext in redisConnectCallback Since the callback is now re-entrant, it can call apis such as redisAsyncDisconnect()
005d7edeb Support calling redisAsyncDisconnect from the onConnected callback, by deferring context deletion
6ed060920 Add async regression test for issue #931
eaa2a7ee7 Merge pull request #932 from kristjanvalur/pr3
2ccef30f3 Add regression test for issue #945
4b901d44a Initial async tests
31c91408e Polling adapter and example
8a15f4d65 Merge pull request #1057 from orgads/static-name
902dd047f Merge pull request #1054 from kristjanvalur/pr08
c78d0926b Merge pull request #1074 from michael-grunder/kristjanvalur-pr4
2b115d56c Whitespace
1343988ce Fix typos
47b57aa24 Add some documentation on connect/disconnect callbacks and command callbacks
a890d9ce2 Merge pull request #1073 from michael-grunder/kristjanvalur-pr1
f246ee433 Whitespace, style
94c1985bd Use correct type for getsockopt()
5e002bc21 Support failed async connects on windows.
5d68ad2f4 Merge pull request #1072 from michael-grunder/fix-redis7-unit-tests
f4b6ed289 Fix tests so they work for Redis 7.0
95a0c1283 Merge pull request #1058 from orgads/win64
eedb37a65 Fix warnings on Win64
47c3ecefc Merge pull request #1062 from yossigo/fix-push-notification-order
e23d91c97 Merge pull request #1061 from yossigo/update-redis-apt
34211ad54 Merge pull request #1063 from redis/fix-windows-tests
9957af7e3 Whitelist hiredis repo path in cygwin
b455b3381 Handle push notifications before or after reply.
aed9ce446 Use official repository for redis package.
d7683f35a Merge pull request #1047 from Nordix/unsubscribe-handling
7c44a9d7e Merge pull request #1045 from Nordix/sds-updates
dd4bf9783 Use the same name for static and shared libraries
ff57c18b9 Embed debug information in windows static lib, rather than create a .pdb file
8310ad4f5 fix cmake version
7123b87f6 Handle any pipelined unsubscribe in async
b6fb548fc Ignore pubsub replies without a channel/pattern
00b82683b Handle overflows as errors instead of asserting
64062a1d4 Catch size_t overflows in sds.c
066c6de79 Use size_t/long to avoid truncation
c6657ef65 Merge branch 'redis:master' into master
50cdcab49 Fix potential fault at createDoubleObject
fd033e983 Remove semicolon after do-while in _EL_CLEANUP
664c415e7 Illumos test fixes, error message difference fot bad hostname test.
git-subtree-dir: deps/hiredis
git-subtree-split: b6a052fe0959dae69e16b9d74449faeb1b70dbe1
2023-05-30 22:23:45 +03:00
|
|
|
size_t reallen = strlen(s);
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdssetlen(s, reallen);
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Modify an hisds string in-place to make it empty (zero length).
|
2020-08-06 12:41:58 -07:00
|
|
|
* However all the existing buffer is not discarded but set as free space
|
|
|
|
* so that next append operations will not require allocations up to the
|
|
|
|
* number of bytes previously available. */
|
2020-08-15 12:24:31 -07:00
|
|
|
void hi_sdsclear(hisds s) {
|
|
|
|
hi_sdssetlen(s, 0);
|
2020-08-06 12:41:58 -07:00
|
|
|
s[0] = '\0';
|
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Enlarge the free space at the end of the hisds string so that the caller
|
2020-08-06 12:41:58 -07:00
|
|
|
* is sure that after calling this function can overwrite up to addlen
|
|
|
|
* bytes after the end of the string, plus one more byte for nul term.
|
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* Note: this does not change the *length* of the hisds string as returned
|
|
|
|
* by hi_sdslen(), but only the free buffer space we have. */
|
|
|
|
hisds hi_sdsMakeRoomFor(hisds s, size_t addlen) {
|
2020-08-06 12:41:58 -07:00
|
|
|
void *sh, *newsh;
|
2020-08-15 12:24:31 -07:00
|
|
|
size_t avail = hi_sdsavail(s);
|
Squashed 'deps/hiredis/' changes from f8de9a4bd..b6a052fe0
b6a052fe0 Helper for setting TCP_USER_TIMEOUT socket option (#1188)
3fa9b6944 Add RedisModule adapter (#1182)
d13c091e9 Fix wincrypt symbols conflict
5d84c8cfd Add a test ensuring we don't clobber connection error.
3f95fcdae Don't attempt to set a timeout if we are in an error state.
aacb84b8d Fix typo in makefile.
563b062e3 Accept -nan per the RESP3 spec recommendation.
04c1b5b02 Fix colliding option values
4ca8e73f6 Rework searching for openssl
cd208812f Attempt to find the correct path for openssl.
011f7093c Allow specifying the keepalive interval
e9243d4f7 Cmake static or shared (#1160)
1cbd5bc76 Write a version file for the CMake package (#1165)
6f5bae8c6 fix typo
acd09461d CMakeLists.txt: respect BUILD_SHARED_LIBS
97fcf0fd1 Add sdevent adapter
ccff093bc Bump dev version for the next release cycle.
c14775b4e Prepare for v1.1.0 GA
f0bdf8405 Add support for nan in RESP3 double (#1133)
991b0b0b3 Add an example that calls redisCommandArgv (#1140)
a36686f84 CI updates (#1139)
8ad4985e9 fix flag reference
7583ebb1b Make freeing a NULL redisAsyncContext a no op.
2c53dea7f Update version in dev branch.
f063370ed Prepare for v1.1.0-rc1
2b069573a CI fixes in preparation of release
e1e9eb40d Add author information to release-drafter template.
afc29ee1a Update for mingw cross compile
ceb8a8815 fixed cpp build error with adapters/libhv.h
3b15a04b5 Fixup of PR734: Coverage of hiredis.c (#1124)
c245df9fb CMake corrections for building on Windows (#1122)
9c338a598 Fix PUSH handler tests for Redis >= 7.0.5
6d5c3ee74 Install on windows fixes (#1117)
68b29e1ad Add timeout support to libhv adapter. (#1109)
722e3409c Additional include directory given by pkg-config (#1118)
bd9ccb8c4 Use __attribute__ when building with clang on windows
5392adc26 set default SSL certificate directory
560e66486 Minor refactor
d756f68a5 Add libhv example to our standard Makefile
a66916719 Add adapters/libhv
855b48a81 Fix pkgconfig for hiredis_ssl
79ae5ffc6 Fix protocol error (#1106)
61b5b299f Use a windows specific keepalive function. (#1104)
fce8abc1c Introduce .close method for redisContextFuncs
cfb6ca881 Add REDIS_OPT_PREFER_UNSPEC (#1101)
cc7c35ce6 Update documentation to explain redisConnectWithOptions.
bc8d837b7 fix heap-buffer-overflow (#957)
ca4a0e850 uvadapter: reduce number of uv_poll_start calls
35d398c90 Fix cmake config path on Linux. CMake config files were installed to `/usr/local/share/hiredis`, which is not recognizable by `find_package()`. I'm not sure why it was set that way. Given the commit introducing it is for Windows, I keep that behavior consistent there, but fix the rest.
10c78c6e1 Add possibility to prefer IPv6, IPv4 or unspecified
1abe0c828 fuzzer: No alloc in redisFormatCommand() when fail
329eaf9ba Fix heap-buffer-overflow issue in redisvFormatCommad
eaae7321c Polling adapter requires sockcompat.h
0a5fa3dde Regression test for off-by-one parsing error
9e174e8f7 Add do while(0) protection for macros
4ad99c69a Rework asSleep to be a generic millisleep function.
75cb6c1ea Do store command timeout in the context for redisSetTimeout (#593)
c57cad658 CMake: remove dict.c form hiredis_sources
8491a65a9 Add Github Actions CI workflow for hiredis: Arm, Arm64, 386, windows. (#943)
77e4f09ea Merge pull request #964 from afcidk/fix-createDoubleObject
9219f7e7c Merge pull request #901 from devnexen/illumos_test_fix
810cc6104 Merge pull request #905 from sundb/master
df8b74d69 Merge pull request #1091 from redis/ssl-error-ub-fix
0ed6cdec3 Fix some undefined behaviour
507a6dcaa Merge pull request #1090 from Nordix/subscribe-oom-error
b044eaa6a Copy error to redisAsyncContext when finding subscribe cb
e0200b797 Merge pull request #1087 from redis/const-and-non-const-callback
6a3e96ad2 Maintain backward compatibiliy withour onConnect callback.
e7afd998f Merge pull request #1079 from SukkaW/drop-macos-10.15-runner
17c8fe079 Merge pull request #931 from kristjanvalur/pr2
b808c0c20 Merge pull request #1083 from chayim/ck-drafter
367a82bf0 Merge pull request #1085 from stanhu/ssl-improve-options-setting
71119a71d Make it possible to set SSL verify mode
dd7979ac1 Merge pull request #1084 from stanhu/sh-improve-ssl-docs
c71116178 Improve example for SSL initialization in README.md
5c9b6b571 Release drafter
a606ccf2a CI: use recommended `vmactions/freebsd-vm@v0`
0865c115b Merge pull request #1080 from Nordix/readme-corrections
f6cee7142 Fix README typos
06be7ff31 Merge pull request #1050 from smmir-cent/fix-cmake-version
7dd833d54 CI: bump macos runner version
f69fac769 Drop `const` on redisAsyncContext in redisConnectCallback Since the callback is now re-entrant, it can call apis such as redisAsyncDisconnect()
005d7edeb Support calling redisAsyncDisconnect from the onConnected callback, by deferring context deletion
6ed060920 Add async regression test for issue #931
eaa2a7ee7 Merge pull request #932 from kristjanvalur/pr3
2ccef30f3 Add regression test for issue #945
4b901d44a Initial async tests
31c91408e Polling adapter and example
8a15f4d65 Merge pull request #1057 from orgads/static-name
902dd047f Merge pull request #1054 from kristjanvalur/pr08
c78d0926b Merge pull request #1074 from michael-grunder/kristjanvalur-pr4
2b115d56c Whitespace
1343988ce Fix typos
47b57aa24 Add some documentation on connect/disconnect callbacks and command callbacks
a890d9ce2 Merge pull request #1073 from michael-grunder/kristjanvalur-pr1
f246ee433 Whitespace, style
94c1985bd Use correct type for getsockopt()
5e002bc21 Support failed async connects on windows.
5d68ad2f4 Merge pull request #1072 from michael-grunder/fix-redis7-unit-tests
f4b6ed289 Fix tests so they work for Redis 7.0
95a0c1283 Merge pull request #1058 from orgads/win64
eedb37a65 Fix warnings on Win64
47c3ecefc Merge pull request #1062 from yossigo/fix-push-notification-order
e23d91c97 Merge pull request #1061 from yossigo/update-redis-apt
34211ad54 Merge pull request #1063 from redis/fix-windows-tests
9957af7e3 Whitelist hiredis repo path in cygwin
b455b3381 Handle push notifications before or after reply.
aed9ce446 Use official repository for redis package.
d7683f35a Merge pull request #1047 from Nordix/unsubscribe-handling
7c44a9d7e Merge pull request #1045 from Nordix/sds-updates
dd4bf9783 Use the same name for static and shared libraries
ff57c18b9 Embed debug information in windows static lib, rather than create a .pdb file
8310ad4f5 fix cmake version
7123b87f6 Handle any pipelined unsubscribe in async
b6fb548fc Ignore pubsub replies without a channel/pattern
00b82683b Handle overflows as errors instead of asserting
64062a1d4 Catch size_t overflows in sds.c
066c6de79 Use size_t/long to avoid truncation
c6657ef65 Merge branch 'redis:master' into master
50cdcab49 Fix potential fault at createDoubleObject
fd033e983 Remove semicolon after do-while in _EL_CLEANUP
664c415e7 Illumos test fixes, error message difference fot bad hostname test.
git-subtree-dir: deps/hiredis
git-subtree-split: b6a052fe0959dae69e16b9d74449faeb1b70dbe1
2023-05-30 22:23:45 +03:00
|
|
|
size_t len, newlen, reqlen;
|
2020-08-15 12:24:31 -07:00
|
|
|
char type, oldtype = s[-1] & HI_SDS_TYPE_MASK;
|
2020-08-06 12:41:58 -07:00
|
|
|
int hdrlen;
|
|
|
|
|
|
|
|
/* Return ASAP if there is enough space left. */
|
|
|
|
if (avail >= addlen) return s;
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
len = hi_sdslen(s);
|
|
|
|
sh = (char*)s-hi_sdsHdrSize(oldtype);
|
Squashed 'deps/hiredis/' changes from f8de9a4bd..b6a052fe0
b6a052fe0 Helper for setting TCP_USER_TIMEOUT socket option (#1188)
3fa9b6944 Add RedisModule adapter (#1182)
d13c091e9 Fix wincrypt symbols conflict
5d84c8cfd Add a test ensuring we don't clobber connection error.
3f95fcdae Don't attempt to set a timeout if we are in an error state.
aacb84b8d Fix typo in makefile.
563b062e3 Accept -nan per the RESP3 spec recommendation.
04c1b5b02 Fix colliding option values
4ca8e73f6 Rework searching for openssl
cd208812f Attempt to find the correct path for openssl.
011f7093c Allow specifying the keepalive interval
e9243d4f7 Cmake static or shared (#1160)
1cbd5bc76 Write a version file for the CMake package (#1165)
6f5bae8c6 fix typo
acd09461d CMakeLists.txt: respect BUILD_SHARED_LIBS
97fcf0fd1 Add sdevent adapter
ccff093bc Bump dev version for the next release cycle.
c14775b4e Prepare for v1.1.0 GA
f0bdf8405 Add support for nan in RESP3 double (#1133)
991b0b0b3 Add an example that calls redisCommandArgv (#1140)
a36686f84 CI updates (#1139)
8ad4985e9 fix flag reference
7583ebb1b Make freeing a NULL redisAsyncContext a no op.
2c53dea7f Update version in dev branch.
f063370ed Prepare for v1.1.0-rc1
2b069573a CI fixes in preparation of release
e1e9eb40d Add author information to release-drafter template.
afc29ee1a Update for mingw cross compile
ceb8a8815 fixed cpp build error with adapters/libhv.h
3b15a04b5 Fixup of PR734: Coverage of hiredis.c (#1124)
c245df9fb CMake corrections for building on Windows (#1122)
9c338a598 Fix PUSH handler tests for Redis >= 7.0.5
6d5c3ee74 Install on windows fixes (#1117)
68b29e1ad Add timeout support to libhv adapter. (#1109)
722e3409c Additional include directory given by pkg-config (#1118)
bd9ccb8c4 Use __attribute__ when building with clang on windows
5392adc26 set default SSL certificate directory
560e66486 Minor refactor
d756f68a5 Add libhv example to our standard Makefile
a66916719 Add adapters/libhv
855b48a81 Fix pkgconfig for hiredis_ssl
79ae5ffc6 Fix protocol error (#1106)
61b5b299f Use a windows specific keepalive function. (#1104)
fce8abc1c Introduce .close method for redisContextFuncs
cfb6ca881 Add REDIS_OPT_PREFER_UNSPEC (#1101)
cc7c35ce6 Update documentation to explain redisConnectWithOptions.
bc8d837b7 fix heap-buffer-overflow (#957)
ca4a0e850 uvadapter: reduce number of uv_poll_start calls
35d398c90 Fix cmake config path on Linux. CMake config files were installed to `/usr/local/share/hiredis`, which is not recognizable by `find_package()`. I'm not sure why it was set that way. Given the commit introducing it is for Windows, I keep that behavior consistent there, but fix the rest.
10c78c6e1 Add possibility to prefer IPv6, IPv4 or unspecified
1abe0c828 fuzzer: No alloc in redisFormatCommand() when fail
329eaf9ba Fix heap-buffer-overflow issue in redisvFormatCommad
eaae7321c Polling adapter requires sockcompat.h
0a5fa3dde Regression test for off-by-one parsing error
9e174e8f7 Add do while(0) protection for macros
4ad99c69a Rework asSleep to be a generic millisleep function.
75cb6c1ea Do store command timeout in the context for redisSetTimeout (#593)
c57cad658 CMake: remove dict.c form hiredis_sources
8491a65a9 Add Github Actions CI workflow for hiredis: Arm, Arm64, 386, windows. (#943)
77e4f09ea Merge pull request #964 from afcidk/fix-createDoubleObject
9219f7e7c Merge pull request #901 from devnexen/illumos_test_fix
810cc6104 Merge pull request #905 from sundb/master
df8b74d69 Merge pull request #1091 from redis/ssl-error-ub-fix
0ed6cdec3 Fix some undefined behaviour
507a6dcaa Merge pull request #1090 from Nordix/subscribe-oom-error
b044eaa6a Copy error to redisAsyncContext when finding subscribe cb
e0200b797 Merge pull request #1087 from redis/const-and-non-const-callback
6a3e96ad2 Maintain backward compatibiliy withour onConnect callback.
e7afd998f Merge pull request #1079 from SukkaW/drop-macos-10.15-runner
17c8fe079 Merge pull request #931 from kristjanvalur/pr2
b808c0c20 Merge pull request #1083 from chayim/ck-drafter
367a82bf0 Merge pull request #1085 from stanhu/ssl-improve-options-setting
71119a71d Make it possible to set SSL verify mode
dd7979ac1 Merge pull request #1084 from stanhu/sh-improve-ssl-docs
c71116178 Improve example for SSL initialization in README.md
5c9b6b571 Release drafter
a606ccf2a CI: use recommended `vmactions/freebsd-vm@v0`
0865c115b Merge pull request #1080 from Nordix/readme-corrections
f6cee7142 Fix README typos
06be7ff31 Merge pull request #1050 from smmir-cent/fix-cmake-version
7dd833d54 CI: bump macos runner version
f69fac769 Drop `const` on redisAsyncContext in redisConnectCallback Since the callback is now re-entrant, it can call apis such as redisAsyncDisconnect()
005d7edeb Support calling redisAsyncDisconnect from the onConnected callback, by deferring context deletion
6ed060920 Add async regression test for issue #931
eaa2a7ee7 Merge pull request #932 from kristjanvalur/pr3
2ccef30f3 Add regression test for issue #945
4b901d44a Initial async tests
31c91408e Polling adapter and example
8a15f4d65 Merge pull request #1057 from orgads/static-name
902dd047f Merge pull request #1054 from kristjanvalur/pr08
c78d0926b Merge pull request #1074 from michael-grunder/kristjanvalur-pr4
2b115d56c Whitespace
1343988ce Fix typos
47b57aa24 Add some documentation on connect/disconnect callbacks and command callbacks
a890d9ce2 Merge pull request #1073 from michael-grunder/kristjanvalur-pr1
f246ee433 Whitespace, style
94c1985bd Use correct type for getsockopt()
5e002bc21 Support failed async connects on windows.
5d68ad2f4 Merge pull request #1072 from michael-grunder/fix-redis7-unit-tests
f4b6ed289 Fix tests so they work for Redis 7.0
95a0c1283 Merge pull request #1058 from orgads/win64
eedb37a65 Fix warnings on Win64
47c3ecefc Merge pull request #1062 from yossigo/fix-push-notification-order
e23d91c97 Merge pull request #1061 from yossigo/update-redis-apt
34211ad54 Merge pull request #1063 from redis/fix-windows-tests
9957af7e3 Whitelist hiredis repo path in cygwin
b455b3381 Handle push notifications before or after reply.
aed9ce446 Use official repository for redis package.
d7683f35a Merge pull request #1047 from Nordix/unsubscribe-handling
7c44a9d7e Merge pull request #1045 from Nordix/sds-updates
dd4bf9783 Use the same name for static and shared libraries
ff57c18b9 Embed debug information in windows static lib, rather than create a .pdb file
8310ad4f5 fix cmake version
7123b87f6 Handle any pipelined unsubscribe in async
b6fb548fc Ignore pubsub replies without a channel/pattern
00b82683b Handle overflows as errors instead of asserting
64062a1d4 Catch size_t overflows in sds.c
066c6de79 Use size_t/long to avoid truncation
c6657ef65 Merge branch 'redis:master' into master
50cdcab49 Fix potential fault at createDoubleObject
fd033e983 Remove semicolon after do-while in _EL_CLEANUP
664c415e7 Illumos test fixes, error message difference fot bad hostname test.
git-subtree-dir: deps/hiredis
git-subtree-split: b6a052fe0959dae69e16b9d74449faeb1b70dbe1
2023-05-30 22:23:45 +03:00
|
|
|
reqlen = newlen = (len+addlen);
|
|
|
|
if (newlen <= len) return NULL; /* Catch size_t overflow */
|
2020-08-15 12:24:31 -07:00
|
|
|
if (newlen < HI_SDS_MAX_PREALLOC)
|
2020-08-06 12:41:58 -07:00
|
|
|
newlen *= 2;
|
|
|
|
else
|
2020-08-15 12:24:31 -07:00
|
|
|
newlen += HI_SDS_MAX_PREALLOC;
|
2020-08-06 12:41:58 -07:00
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
type = hi_sdsReqType(newlen);
|
2020-08-06 12:41:58 -07:00
|
|
|
|
|
|
|
/* Don't use type 5: the user is appending to the string and type 5 is
|
2020-08-15 12:24:31 -07:00
|
|
|
* not able to remember empty space, so hi_sdsMakeRoomFor() must be called
|
2020-08-06 12:41:58 -07:00
|
|
|
* at every appending operation. */
|
2020-08-15 12:24:31 -07:00
|
|
|
if (type == HI_SDS_TYPE_5) type = HI_SDS_TYPE_8;
|
2020-08-06 12:41:58 -07:00
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
hdrlen = hi_sdsHdrSize(type);
|
Squashed 'deps/hiredis/' changes from f8de9a4bd..b6a052fe0
b6a052fe0 Helper for setting TCP_USER_TIMEOUT socket option (#1188)
3fa9b6944 Add RedisModule adapter (#1182)
d13c091e9 Fix wincrypt symbols conflict
5d84c8cfd Add a test ensuring we don't clobber connection error.
3f95fcdae Don't attempt to set a timeout if we are in an error state.
aacb84b8d Fix typo in makefile.
563b062e3 Accept -nan per the RESP3 spec recommendation.
04c1b5b02 Fix colliding option values
4ca8e73f6 Rework searching for openssl
cd208812f Attempt to find the correct path for openssl.
011f7093c Allow specifying the keepalive interval
e9243d4f7 Cmake static or shared (#1160)
1cbd5bc76 Write a version file for the CMake package (#1165)
6f5bae8c6 fix typo
acd09461d CMakeLists.txt: respect BUILD_SHARED_LIBS
97fcf0fd1 Add sdevent adapter
ccff093bc Bump dev version for the next release cycle.
c14775b4e Prepare for v1.1.0 GA
f0bdf8405 Add support for nan in RESP3 double (#1133)
991b0b0b3 Add an example that calls redisCommandArgv (#1140)
a36686f84 CI updates (#1139)
8ad4985e9 fix flag reference
7583ebb1b Make freeing a NULL redisAsyncContext a no op.
2c53dea7f Update version in dev branch.
f063370ed Prepare for v1.1.0-rc1
2b069573a CI fixes in preparation of release
e1e9eb40d Add author information to release-drafter template.
afc29ee1a Update for mingw cross compile
ceb8a8815 fixed cpp build error with adapters/libhv.h
3b15a04b5 Fixup of PR734: Coverage of hiredis.c (#1124)
c245df9fb CMake corrections for building on Windows (#1122)
9c338a598 Fix PUSH handler tests for Redis >= 7.0.5
6d5c3ee74 Install on windows fixes (#1117)
68b29e1ad Add timeout support to libhv adapter. (#1109)
722e3409c Additional include directory given by pkg-config (#1118)
bd9ccb8c4 Use __attribute__ when building with clang on windows
5392adc26 set default SSL certificate directory
560e66486 Minor refactor
d756f68a5 Add libhv example to our standard Makefile
a66916719 Add adapters/libhv
855b48a81 Fix pkgconfig for hiredis_ssl
79ae5ffc6 Fix protocol error (#1106)
61b5b299f Use a windows specific keepalive function. (#1104)
fce8abc1c Introduce .close method for redisContextFuncs
cfb6ca881 Add REDIS_OPT_PREFER_UNSPEC (#1101)
cc7c35ce6 Update documentation to explain redisConnectWithOptions.
bc8d837b7 fix heap-buffer-overflow (#957)
ca4a0e850 uvadapter: reduce number of uv_poll_start calls
35d398c90 Fix cmake config path on Linux. CMake config files were installed to `/usr/local/share/hiredis`, which is not recognizable by `find_package()`. I'm not sure why it was set that way. Given the commit introducing it is for Windows, I keep that behavior consistent there, but fix the rest.
10c78c6e1 Add possibility to prefer IPv6, IPv4 or unspecified
1abe0c828 fuzzer: No alloc in redisFormatCommand() when fail
329eaf9ba Fix heap-buffer-overflow issue in redisvFormatCommad
eaae7321c Polling adapter requires sockcompat.h
0a5fa3dde Regression test for off-by-one parsing error
9e174e8f7 Add do while(0) protection for macros
4ad99c69a Rework asSleep to be a generic millisleep function.
75cb6c1ea Do store command timeout in the context for redisSetTimeout (#593)
c57cad658 CMake: remove dict.c form hiredis_sources
8491a65a9 Add Github Actions CI workflow for hiredis: Arm, Arm64, 386, windows. (#943)
77e4f09ea Merge pull request #964 from afcidk/fix-createDoubleObject
9219f7e7c Merge pull request #901 from devnexen/illumos_test_fix
810cc6104 Merge pull request #905 from sundb/master
df8b74d69 Merge pull request #1091 from redis/ssl-error-ub-fix
0ed6cdec3 Fix some undefined behaviour
507a6dcaa Merge pull request #1090 from Nordix/subscribe-oom-error
b044eaa6a Copy error to redisAsyncContext when finding subscribe cb
e0200b797 Merge pull request #1087 from redis/const-and-non-const-callback
6a3e96ad2 Maintain backward compatibiliy withour onConnect callback.
e7afd998f Merge pull request #1079 from SukkaW/drop-macos-10.15-runner
17c8fe079 Merge pull request #931 from kristjanvalur/pr2
b808c0c20 Merge pull request #1083 from chayim/ck-drafter
367a82bf0 Merge pull request #1085 from stanhu/ssl-improve-options-setting
71119a71d Make it possible to set SSL verify mode
dd7979ac1 Merge pull request #1084 from stanhu/sh-improve-ssl-docs
c71116178 Improve example for SSL initialization in README.md
5c9b6b571 Release drafter
a606ccf2a CI: use recommended `vmactions/freebsd-vm@v0`
0865c115b Merge pull request #1080 from Nordix/readme-corrections
f6cee7142 Fix README typos
06be7ff31 Merge pull request #1050 from smmir-cent/fix-cmake-version
7dd833d54 CI: bump macos runner version
f69fac769 Drop `const` on redisAsyncContext in redisConnectCallback Since the callback is now re-entrant, it can call apis such as redisAsyncDisconnect()
005d7edeb Support calling redisAsyncDisconnect from the onConnected callback, by deferring context deletion
6ed060920 Add async regression test for issue #931
eaa2a7ee7 Merge pull request #932 from kristjanvalur/pr3
2ccef30f3 Add regression test for issue #945
4b901d44a Initial async tests
31c91408e Polling adapter and example
8a15f4d65 Merge pull request #1057 from orgads/static-name
902dd047f Merge pull request #1054 from kristjanvalur/pr08
c78d0926b Merge pull request #1074 from michael-grunder/kristjanvalur-pr4
2b115d56c Whitespace
1343988ce Fix typos
47b57aa24 Add some documentation on connect/disconnect callbacks and command callbacks
a890d9ce2 Merge pull request #1073 from michael-grunder/kristjanvalur-pr1
f246ee433 Whitespace, style
94c1985bd Use correct type for getsockopt()
5e002bc21 Support failed async connects on windows.
5d68ad2f4 Merge pull request #1072 from michael-grunder/fix-redis7-unit-tests
f4b6ed289 Fix tests so they work for Redis 7.0
95a0c1283 Merge pull request #1058 from orgads/win64
eedb37a65 Fix warnings on Win64
47c3ecefc Merge pull request #1062 from yossigo/fix-push-notification-order
e23d91c97 Merge pull request #1061 from yossigo/update-redis-apt
34211ad54 Merge pull request #1063 from redis/fix-windows-tests
9957af7e3 Whitelist hiredis repo path in cygwin
b455b3381 Handle push notifications before or after reply.
aed9ce446 Use official repository for redis package.
d7683f35a Merge pull request #1047 from Nordix/unsubscribe-handling
7c44a9d7e Merge pull request #1045 from Nordix/sds-updates
dd4bf9783 Use the same name for static and shared libraries
ff57c18b9 Embed debug information in windows static lib, rather than create a .pdb file
8310ad4f5 fix cmake version
7123b87f6 Handle any pipelined unsubscribe in async
b6fb548fc Ignore pubsub replies without a channel/pattern
00b82683b Handle overflows as errors instead of asserting
64062a1d4 Catch size_t overflows in sds.c
066c6de79 Use size_t/long to avoid truncation
c6657ef65 Merge branch 'redis:master' into master
50cdcab49 Fix potential fault at createDoubleObject
fd033e983 Remove semicolon after do-while in _EL_CLEANUP
664c415e7 Illumos test fixes, error message difference fot bad hostname test.
git-subtree-dir: deps/hiredis
git-subtree-split: b6a052fe0959dae69e16b9d74449faeb1b70dbe1
2023-05-30 22:23:45 +03:00
|
|
|
if (hdrlen+newlen+1 <= reqlen) return NULL; /* Catch size_t overflow */
|
2020-08-06 12:41:58 -07:00
|
|
|
if (oldtype==type) {
|
2020-08-15 12:24:31 -07:00
|
|
|
newsh = hi_s_realloc(sh, hdrlen+newlen+1);
|
2020-08-06 12:41:58 -07:00
|
|
|
if (newsh == NULL) return NULL;
|
|
|
|
s = (char*)newsh+hdrlen;
|
|
|
|
} else {
|
|
|
|
/* Since the header size changes, need to move the string forward,
|
|
|
|
* and can't use realloc */
|
2020-08-15 12:24:31 -07:00
|
|
|
newsh = hi_s_malloc(hdrlen+newlen+1);
|
2020-08-06 12:41:58 -07:00
|
|
|
if (newsh == NULL) return NULL;
|
|
|
|
memcpy((char*)newsh+hdrlen, s, len+1);
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_s_free(sh);
|
2020-08-06 12:41:58 -07:00
|
|
|
s = (char*)newsh+hdrlen;
|
|
|
|
s[-1] = type;
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdssetlen(s, len);
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdssetalloc(s, newlen);
|
2020-08-06 12:41:58 -07:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Reallocate the hisds string so that it has no free space at the end. The
|
2020-08-06 12:41:58 -07:00
|
|
|
* contained string remains not altered, but next concatenation operations
|
|
|
|
* will require a reallocation.
|
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* After the call, the passed hisds string is no longer valid and all the
|
2020-08-06 12:41:58 -07:00
|
|
|
* references must be substituted with the new pointer returned by the call. */
|
2020-08-15 12:24:31 -07:00
|
|
|
hisds hi_sdsRemoveFreeSpace(hisds s) {
|
2020-08-06 12:41:58 -07:00
|
|
|
void *sh, *newsh;
|
2020-08-15 12:24:31 -07:00
|
|
|
char type, oldtype = s[-1] & HI_SDS_TYPE_MASK;
|
2020-08-06 12:41:58 -07:00
|
|
|
int hdrlen;
|
2020-08-15 12:24:31 -07:00
|
|
|
size_t len = hi_sdslen(s);
|
|
|
|
sh = (char*)s-hi_sdsHdrSize(oldtype);
|
2020-08-06 12:41:58 -07:00
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
type = hi_sdsReqType(len);
|
|
|
|
hdrlen = hi_sdsHdrSize(type);
|
2020-08-06 12:41:58 -07:00
|
|
|
if (oldtype==type) {
|
2020-08-15 12:24:31 -07:00
|
|
|
newsh = hi_s_realloc(sh, hdrlen+len+1);
|
2020-08-06 12:41:58 -07:00
|
|
|
if (newsh == NULL) return NULL;
|
|
|
|
s = (char*)newsh+hdrlen;
|
|
|
|
} else {
|
2020-08-15 12:24:31 -07:00
|
|
|
newsh = hi_s_malloc(hdrlen+len+1);
|
2020-08-06 12:41:58 -07:00
|
|
|
if (newsh == NULL) return NULL;
|
|
|
|
memcpy((char*)newsh+hdrlen, s, len+1);
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_s_free(sh);
|
2020-08-06 12:41:58 -07:00
|
|
|
s = (char*)newsh+hdrlen;
|
|
|
|
s[-1] = type;
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdssetlen(s, len);
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdssetalloc(s, len);
|
2020-08-06 12:41:58 -07:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Return the total size of the allocation of the specifed hisds string,
|
2020-08-06 12:41:58 -07:00
|
|
|
* including:
|
2020-08-15 12:24:31 -07:00
|
|
|
* 1) The hisds header before the pointer.
|
2020-08-06 12:41:58 -07:00
|
|
|
* 2) The string.
|
|
|
|
* 3) The free buffer at the end if any.
|
|
|
|
* 4) The implicit null term.
|
|
|
|
*/
|
2020-08-15 12:24:31 -07:00
|
|
|
size_t hi_sdsAllocSize(hisds s) {
|
|
|
|
size_t alloc = hi_sdsalloc(s);
|
|
|
|
return hi_sdsHdrSize(s[-1])+alloc+1;
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the pointer of the actual SDS allocation (normally SDS strings
|
|
|
|
* are referenced by the start of the string buffer). */
|
2020-08-15 12:24:31 -07:00
|
|
|
void *hi_sdsAllocPtr(hisds s) {
|
|
|
|
return (void*) (s-hi_sdsHdrSize(s[-1]));
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Increment the hisds length and decrements the left free space at the
|
2020-08-06 12:41:58 -07:00
|
|
|
* end of the string according to 'incr'. Also set the null term
|
|
|
|
* in the new end of the string.
|
|
|
|
*
|
|
|
|
* This function is used in order to fix the string length after the
|
2020-08-15 12:24:31 -07:00
|
|
|
* user calls hi_sdsMakeRoomFor(), writes something after the end of
|
2020-08-06 12:41:58 -07:00
|
|
|
* the current string, and finally needs to set the new length.
|
|
|
|
*
|
|
|
|
* Note: it is possible to use a negative increment in order to
|
|
|
|
* right-trim the string.
|
|
|
|
*
|
|
|
|
* Usage example:
|
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* Using hi_sdsIncrLen() and hi_sdsMakeRoomFor() it is possible to mount the
|
2020-08-06 12:41:58 -07:00
|
|
|
* following schema, to cat bytes coming from the kernel to the end of an
|
2020-08-15 12:24:31 -07:00
|
|
|
* hisds string without copying into an intermediate buffer:
|
2020-08-06 12:41:58 -07:00
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* oldlen = hi_hi_sdslen(s);
|
|
|
|
* s = hi_sdsMakeRoomFor(s, BUFFER_SIZE);
|
2020-08-06 12:41:58 -07:00
|
|
|
* nread = read(fd, s+oldlen, BUFFER_SIZE);
|
|
|
|
* ... check for nread <= 0 and handle it ...
|
2020-08-15 12:24:31 -07:00
|
|
|
* hi_sdsIncrLen(s, nread);
|
2020-08-06 12:41:58 -07:00
|
|
|
*/
|
2020-08-15 12:24:31 -07:00
|
|
|
void hi_sdsIncrLen(hisds s, int incr) {
|
2020-08-06 12:41:58 -07:00
|
|
|
unsigned char flags = s[-1];
|
|
|
|
size_t len;
|
2020-08-15 12:24:31 -07:00
|
|
|
switch(flags&HI_SDS_TYPE_MASK) {
|
|
|
|
case HI_SDS_TYPE_5: {
|
2020-08-06 12:41:58 -07:00
|
|
|
unsigned char *fp = ((unsigned char*)s)-1;
|
2020-08-15 12:24:31 -07:00
|
|
|
unsigned char oldlen = HI_SDS_TYPE_5_LEN(flags);
|
2020-08-06 12:41:58 -07:00
|
|
|
assert((incr > 0 && oldlen+incr < 32) || (incr < 0 && oldlen >= (unsigned int)(-incr)));
|
2020-08-15 12:24:31 -07:00
|
|
|
*fp = HI_SDS_TYPE_5 | ((oldlen+incr) << HI_SDS_TYPE_BITS);
|
2020-08-06 12:41:58 -07:00
|
|
|
len = oldlen+incr;
|
|
|
|
break;
|
|
|
|
}
|
2020-08-15 12:24:31 -07:00
|
|
|
case HI_SDS_TYPE_8: {
|
|
|
|
HI_SDS_HDR_VAR(8,s);
|
2020-08-06 12:41:58 -07:00
|
|
|
assert((incr >= 0 && sh->alloc-sh->len >= incr) || (incr < 0 && sh->len >= (unsigned int)(-incr)));
|
|
|
|
len = (sh->len += incr);
|
|
|
|
break;
|
|
|
|
}
|
2020-08-15 12:24:31 -07:00
|
|
|
case HI_SDS_TYPE_16: {
|
|
|
|
HI_SDS_HDR_VAR(16,s);
|
2020-08-06 12:41:58 -07:00
|
|
|
assert((incr >= 0 && sh->alloc-sh->len >= incr) || (incr < 0 && sh->len >= (unsigned int)(-incr)));
|
|
|
|
len = (sh->len += incr);
|
|
|
|
break;
|
|
|
|
}
|
2020-08-15 12:24:31 -07:00
|
|
|
case HI_SDS_TYPE_32: {
|
|
|
|
HI_SDS_HDR_VAR(32,s);
|
2020-08-06 12:41:58 -07:00
|
|
|
assert((incr >= 0 && sh->alloc-sh->len >= (unsigned int)incr) || (incr < 0 && sh->len >= (unsigned int)(-incr)));
|
|
|
|
len = (sh->len += incr);
|
|
|
|
break;
|
|
|
|
}
|
2020-08-15 12:24:31 -07:00
|
|
|
case HI_SDS_TYPE_64: {
|
|
|
|
HI_SDS_HDR_VAR(64,s);
|
2020-08-06 12:41:58 -07:00
|
|
|
assert((incr >= 0 && sh->alloc-sh->len >= (uint64_t)incr) || (incr < 0 && sh->len >= (uint64_t)(-incr)));
|
|
|
|
len = (sh->len += incr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: len = 0; /* Just to avoid compilation warnings. */
|
|
|
|
}
|
|
|
|
s[len] = '\0';
|
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Grow the hisds to have the specified length. Bytes that were not part of
|
|
|
|
* the original length of the hisds will be set to zero.
|
2020-08-06 12:41:58 -07:00
|
|
|
*
|
|
|
|
* if the specified length is smaller than the current length, no operation
|
|
|
|
* is performed. */
|
2020-08-15 12:24:31 -07:00
|
|
|
hisds hi_sdsgrowzero(hisds s, size_t len) {
|
|
|
|
size_t curlen = hi_sdslen(s);
|
2020-08-06 12:41:58 -07:00
|
|
|
|
|
|
|
if (len <= curlen) return s;
|
2020-08-15 12:24:31 -07:00
|
|
|
s = hi_sdsMakeRoomFor(s,len-curlen);
|
2020-08-06 12:41:58 -07:00
|
|
|
if (s == NULL) return NULL;
|
|
|
|
|
|
|
|
/* Make sure added region doesn't contain garbage */
|
|
|
|
memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdssetlen(s, len);
|
2020-08-06 12:41:58 -07:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append the specified binary-safe string pointed by 't' of 'len' bytes to the
|
2020-08-15 12:24:31 -07:00
|
|
|
* end of the specified hisds string 's'.
|
2020-08-06 12:41:58 -07:00
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* After the call, the passed hisds string is no longer valid and all the
|
2020-08-06 12:41:58 -07:00
|
|
|
* references must be substituted with the new pointer returned by the call. */
|
2020-08-15 12:24:31 -07:00
|
|
|
hisds hi_sdscatlen(hisds s, const void *t, size_t len) {
|
|
|
|
size_t curlen = hi_sdslen(s);
|
2020-08-06 12:41:58 -07:00
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
s = hi_sdsMakeRoomFor(s,len);
|
2020-08-06 12:41:58 -07:00
|
|
|
if (s == NULL) return NULL;
|
|
|
|
memcpy(s+curlen, t, len);
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdssetlen(s, curlen+len);
|
2020-08-06 12:41:58 -07:00
|
|
|
s[curlen+len] = '\0';
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Append the specified null termianted C string to the hisds string 's'.
|
2020-08-06 12:41:58 -07:00
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* After the call, the passed hisds string is no longer valid and all the
|
2020-08-06 12:41:58 -07:00
|
|
|
* references must be substituted with the new pointer returned by the call. */
|
2020-08-15 12:24:31 -07:00
|
|
|
hisds hi_sdscat(hisds s, const char *t) {
|
|
|
|
return hi_sdscatlen(s, t, strlen(t));
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Append the specified hisds 't' to the existing hisds 's'.
|
2020-08-06 12:41:58 -07:00
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* After the call, the modified hisds string is no longer valid and all the
|
2020-08-06 12:41:58 -07:00
|
|
|
* references must be substituted with the new pointer returned by the call. */
|
2020-08-15 12:24:31 -07:00
|
|
|
hisds hi_sdscatsds(hisds s, const hisds t) {
|
|
|
|
return hi_sdscatlen(s, t, hi_sdslen(t));
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Destructively modify the hisds string 's' to hold the specified binary
|
2020-08-06 12:41:58 -07:00
|
|
|
* safe string pointed by 't' of length 'len' bytes. */
|
2020-08-15 12:24:31 -07:00
|
|
|
hisds hi_sdscpylen(hisds s, const char *t, size_t len) {
|
|
|
|
if (hi_sdsalloc(s) < len) {
|
|
|
|
s = hi_sdsMakeRoomFor(s,len-hi_sdslen(s));
|
2020-08-06 12:41:58 -07:00
|
|
|
if (s == NULL) return NULL;
|
|
|
|
}
|
|
|
|
memcpy(s, t, len);
|
|
|
|
s[len] = '\0';
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdssetlen(s, len);
|
2020-08-06 12:41:58 -07:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Like hi_sdscpylen() but 't' must be a null-terminated string so that the length
|
2020-08-06 12:41:58 -07:00
|
|
|
* of the string is obtained with strlen(). */
|
2020-08-15 12:24:31 -07:00
|
|
|
hisds hi_sdscpy(hisds s, const char *t) {
|
|
|
|
return hi_sdscpylen(s, t, strlen(t));
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Helper for hi_sdscatlonglong() doing the actual number -> string
|
2020-08-06 12:41:58 -07:00
|
|
|
* conversion. 's' must point to a string with room for at least
|
2020-08-15 12:24:31 -07:00
|
|
|
* HI_SDS_LLSTR_SIZE bytes.
|
2020-08-06 12:41:58 -07:00
|
|
|
*
|
|
|
|
* The function returns the length of the null-terminated string
|
|
|
|
* representation stored at 's'. */
|
2020-08-15 12:24:31 -07:00
|
|
|
#define HI_SDS_LLSTR_SIZE 21
|
|
|
|
int hi_sdsll2str(char *s, long long value) {
|
2020-08-06 12:41:58 -07:00
|
|
|
char *p, aux;
|
|
|
|
unsigned long long v;
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
/* Generate the string representation, this method produces
|
|
|
|
* an reversed string. */
|
|
|
|
v = (value < 0) ? -value : value;
|
|
|
|
p = s;
|
|
|
|
do {
|
|
|
|
*p++ = '0'+(v%10);
|
|
|
|
v /= 10;
|
|
|
|
} while(v);
|
|
|
|
if (value < 0) *p++ = '-';
|
|
|
|
|
|
|
|
/* Compute length and add null term. */
|
|
|
|
l = p-s;
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
/* Reverse the string. */
|
|
|
|
p--;
|
|
|
|
while(s < p) {
|
|
|
|
aux = *s;
|
|
|
|
*s = *p;
|
|
|
|
*p = aux;
|
|
|
|
s++;
|
|
|
|
p--;
|
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Identical hi_sdsll2str(), but for unsigned long long type. */
|
|
|
|
int hi_sdsull2str(char *s, unsigned long long v) {
|
2020-08-06 12:41:58 -07:00
|
|
|
char *p, aux;
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
/* Generate the string representation, this method produces
|
|
|
|
* an reversed string. */
|
|
|
|
p = s;
|
|
|
|
do {
|
|
|
|
*p++ = '0'+(v%10);
|
|
|
|
v /= 10;
|
|
|
|
} while(v);
|
|
|
|
|
|
|
|
/* Compute length and add null term. */
|
|
|
|
l = p-s;
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
/* Reverse the string. */
|
|
|
|
p--;
|
|
|
|
while(s < p) {
|
|
|
|
aux = *s;
|
|
|
|
*s = *p;
|
|
|
|
*p = aux;
|
|
|
|
s++;
|
|
|
|
p--;
|
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Create an hisds string from a long long value. It is much faster than:
|
2020-08-06 12:41:58 -07:00
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* hi_sdscatprintf(hi_sdsempty(),"%lld\n", value);
|
2020-08-06 12:41:58 -07:00
|
|
|
*/
|
2020-08-15 12:24:31 -07:00
|
|
|
hisds hi_sdsfromlonglong(long long value) {
|
|
|
|
char buf[HI_SDS_LLSTR_SIZE];
|
|
|
|
int len = hi_sdsll2str(buf,value);
|
2020-08-06 12:41:58 -07:00
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
return hi_sdsnewlen(buf,len);
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Like hi_sdscatprintf() but gets va_list instead of being variadic. */
|
|
|
|
hisds hi_sdscatvprintf(hisds s, const char *fmt, va_list ap) {
|
2020-08-06 12:41:58 -07:00
|
|
|
va_list cpy;
|
|
|
|
char staticbuf[1024], *buf = staticbuf, *t;
|
|
|
|
size_t buflen = strlen(fmt)*2;
|
|
|
|
|
|
|
|
/* We try to start using a static buffer for speed.
|
|
|
|
* If not possible we revert to heap allocation. */
|
|
|
|
if (buflen > sizeof(staticbuf)) {
|
2020-08-15 12:24:31 -07:00
|
|
|
buf = hi_s_malloc(buflen);
|
2020-08-06 12:41:58 -07:00
|
|
|
if (buf == NULL) return NULL;
|
|
|
|
} else {
|
|
|
|
buflen = sizeof(staticbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try with buffers two times bigger every time we fail to
|
|
|
|
* fit the string in the current buffer size. */
|
|
|
|
while(1) {
|
|
|
|
buf[buflen-2] = '\0';
|
|
|
|
va_copy(cpy,ap);
|
|
|
|
vsnprintf(buf, buflen, fmt, cpy);
|
|
|
|
va_end(cpy);
|
|
|
|
if (buf[buflen-2] != '\0') {
|
2020-08-15 12:24:31 -07:00
|
|
|
if (buf != staticbuf) hi_s_free(buf);
|
2020-08-06 12:41:58 -07:00
|
|
|
buflen *= 2;
|
2020-08-15 12:24:31 -07:00
|
|
|
buf = hi_s_malloc(buflen);
|
2020-08-06 12:41:58 -07:00
|
|
|
if (buf == NULL) return NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally concat the obtained string to the SDS string and return it. */
|
2020-08-15 12:24:31 -07:00
|
|
|
t = hi_sdscat(s, buf);
|
|
|
|
if (buf != staticbuf) hi_s_free(buf);
|
2020-08-06 12:41:58 -07:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Append to the hisds string 's' a string obtained using printf-alike format
|
2020-08-06 12:41:58 -07:00
|
|
|
* specifier.
|
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* After the call, the modified hisds string is no longer valid and all the
|
2020-08-06 12:41:58 -07:00
|
|
|
* references must be substituted with the new pointer returned by the call.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* s = hi_sdsnew("Sum is: ");
|
|
|
|
* s = hi_sdscatprintf(s,"%d+%d = %d",a,b,a+b).
|
2020-08-06 12:41:58 -07:00
|
|
|
*
|
|
|
|
* Often you need to create a string from scratch with the printf-alike
|
2020-08-15 12:24:31 -07:00
|
|
|
* format. When this is the need, just use hi_sdsempty() as the target string:
|
2020-08-06 12:41:58 -07:00
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* s = hi_sdscatprintf(hi_sdsempty(), "... your format ...", args);
|
2020-08-06 12:41:58 -07:00
|
|
|
*/
|
2020-08-15 12:24:31 -07:00
|
|
|
hisds hi_sdscatprintf(hisds s, const char *fmt, ...) {
|
2020-08-06 12:41:58 -07:00
|
|
|
va_list ap;
|
|
|
|
char *t;
|
|
|
|
va_start(ap, fmt);
|
2020-08-15 12:24:31 -07:00
|
|
|
t = hi_sdscatvprintf(s,fmt,ap);
|
2020-08-06 12:41:58 -07:00
|
|
|
va_end(ap);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* This function is similar to hi_sdscatprintf, but much faster as it does
|
2020-08-06 12:41:58 -07:00
|
|
|
* not rely on sprintf() family functions implemented by the libc that
|
2020-08-15 12:24:31 -07:00
|
|
|
* are often very slow. Moreover directly handling the hisds string as
|
2020-08-06 12:41:58 -07:00
|
|
|
* new data is concatenated provides a performance improvement.
|
|
|
|
*
|
|
|
|
* However this function only handles an incompatible subset of printf-alike
|
|
|
|
* format specifiers:
|
|
|
|
*
|
|
|
|
* %s - C String
|
|
|
|
* %S - SDS string
|
|
|
|
* %i - signed int
|
|
|
|
* %I - 64 bit signed integer (long long, int64_t)
|
|
|
|
* %u - unsigned int
|
|
|
|
* %U - 64 bit unsigned integer (unsigned long long, uint64_t)
|
|
|
|
* %% - Verbatim "%" character.
|
|
|
|
*/
|
2020-08-15 12:24:31 -07:00
|
|
|
hisds hi_sdscatfmt(hisds s, char const *fmt, ...) {
|
2020-08-06 12:41:58 -07:00
|
|
|
const char *f = fmt;
|
Squashed 'deps/hiredis/' changes from f8de9a4bd..b6a052fe0
b6a052fe0 Helper for setting TCP_USER_TIMEOUT socket option (#1188)
3fa9b6944 Add RedisModule adapter (#1182)
d13c091e9 Fix wincrypt symbols conflict
5d84c8cfd Add a test ensuring we don't clobber connection error.
3f95fcdae Don't attempt to set a timeout if we are in an error state.
aacb84b8d Fix typo in makefile.
563b062e3 Accept -nan per the RESP3 spec recommendation.
04c1b5b02 Fix colliding option values
4ca8e73f6 Rework searching for openssl
cd208812f Attempt to find the correct path for openssl.
011f7093c Allow specifying the keepalive interval
e9243d4f7 Cmake static or shared (#1160)
1cbd5bc76 Write a version file for the CMake package (#1165)
6f5bae8c6 fix typo
acd09461d CMakeLists.txt: respect BUILD_SHARED_LIBS
97fcf0fd1 Add sdevent adapter
ccff093bc Bump dev version for the next release cycle.
c14775b4e Prepare for v1.1.0 GA
f0bdf8405 Add support for nan in RESP3 double (#1133)
991b0b0b3 Add an example that calls redisCommandArgv (#1140)
a36686f84 CI updates (#1139)
8ad4985e9 fix flag reference
7583ebb1b Make freeing a NULL redisAsyncContext a no op.
2c53dea7f Update version in dev branch.
f063370ed Prepare for v1.1.0-rc1
2b069573a CI fixes in preparation of release
e1e9eb40d Add author information to release-drafter template.
afc29ee1a Update for mingw cross compile
ceb8a8815 fixed cpp build error with adapters/libhv.h
3b15a04b5 Fixup of PR734: Coverage of hiredis.c (#1124)
c245df9fb CMake corrections for building on Windows (#1122)
9c338a598 Fix PUSH handler tests for Redis >= 7.0.5
6d5c3ee74 Install on windows fixes (#1117)
68b29e1ad Add timeout support to libhv adapter. (#1109)
722e3409c Additional include directory given by pkg-config (#1118)
bd9ccb8c4 Use __attribute__ when building with clang on windows
5392adc26 set default SSL certificate directory
560e66486 Minor refactor
d756f68a5 Add libhv example to our standard Makefile
a66916719 Add adapters/libhv
855b48a81 Fix pkgconfig for hiredis_ssl
79ae5ffc6 Fix protocol error (#1106)
61b5b299f Use a windows specific keepalive function. (#1104)
fce8abc1c Introduce .close method for redisContextFuncs
cfb6ca881 Add REDIS_OPT_PREFER_UNSPEC (#1101)
cc7c35ce6 Update documentation to explain redisConnectWithOptions.
bc8d837b7 fix heap-buffer-overflow (#957)
ca4a0e850 uvadapter: reduce number of uv_poll_start calls
35d398c90 Fix cmake config path on Linux. CMake config files were installed to `/usr/local/share/hiredis`, which is not recognizable by `find_package()`. I'm not sure why it was set that way. Given the commit introducing it is for Windows, I keep that behavior consistent there, but fix the rest.
10c78c6e1 Add possibility to prefer IPv6, IPv4 or unspecified
1abe0c828 fuzzer: No alloc in redisFormatCommand() when fail
329eaf9ba Fix heap-buffer-overflow issue in redisvFormatCommad
eaae7321c Polling adapter requires sockcompat.h
0a5fa3dde Regression test for off-by-one parsing error
9e174e8f7 Add do while(0) protection for macros
4ad99c69a Rework asSleep to be a generic millisleep function.
75cb6c1ea Do store command timeout in the context for redisSetTimeout (#593)
c57cad658 CMake: remove dict.c form hiredis_sources
8491a65a9 Add Github Actions CI workflow for hiredis: Arm, Arm64, 386, windows. (#943)
77e4f09ea Merge pull request #964 from afcidk/fix-createDoubleObject
9219f7e7c Merge pull request #901 from devnexen/illumos_test_fix
810cc6104 Merge pull request #905 from sundb/master
df8b74d69 Merge pull request #1091 from redis/ssl-error-ub-fix
0ed6cdec3 Fix some undefined behaviour
507a6dcaa Merge pull request #1090 from Nordix/subscribe-oom-error
b044eaa6a Copy error to redisAsyncContext when finding subscribe cb
e0200b797 Merge pull request #1087 from redis/const-and-non-const-callback
6a3e96ad2 Maintain backward compatibiliy withour onConnect callback.
e7afd998f Merge pull request #1079 from SukkaW/drop-macos-10.15-runner
17c8fe079 Merge pull request #931 from kristjanvalur/pr2
b808c0c20 Merge pull request #1083 from chayim/ck-drafter
367a82bf0 Merge pull request #1085 from stanhu/ssl-improve-options-setting
71119a71d Make it possible to set SSL verify mode
dd7979ac1 Merge pull request #1084 from stanhu/sh-improve-ssl-docs
c71116178 Improve example for SSL initialization in README.md
5c9b6b571 Release drafter
a606ccf2a CI: use recommended `vmactions/freebsd-vm@v0`
0865c115b Merge pull request #1080 from Nordix/readme-corrections
f6cee7142 Fix README typos
06be7ff31 Merge pull request #1050 from smmir-cent/fix-cmake-version
7dd833d54 CI: bump macos runner version
f69fac769 Drop `const` on redisAsyncContext in redisConnectCallback Since the callback is now re-entrant, it can call apis such as redisAsyncDisconnect()
005d7edeb Support calling redisAsyncDisconnect from the onConnected callback, by deferring context deletion
6ed060920 Add async regression test for issue #931
eaa2a7ee7 Merge pull request #932 from kristjanvalur/pr3
2ccef30f3 Add regression test for issue #945
4b901d44a Initial async tests
31c91408e Polling adapter and example
8a15f4d65 Merge pull request #1057 from orgads/static-name
902dd047f Merge pull request #1054 from kristjanvalur/pr08
c78d0926b Merge pull request #1074 from michael-grunder/kristjanvalur-pr4
2b115d56c Whitespace
1343988ce Fix typos
47b57aa24 Add some documentation on connect/disconnect callbacks and command callbacks
a890d9ce2 Merge pull request #1073 from michael-grunder/kristjanvalur-pr1
f246ee433 Whitespace, style
94c1985bd Use correct type for getsockopt()
5e002bc21 Support failed async connects on windows.
5d68ad2f4 Merge pull request #1072 from michael-grunder/fix-redis7-unit-tests
f4b6ed289 Fix tests so they work for Redis 7.0
95a0c1283 Merge pull request #1058 from orgads/win64
eedb37a65 Fix warnings on Win64
47c3ecefc Merge pull request #1062 from yossigo/fix-push-notification-order
e23d91c97 Merge pull request #1061 from yossigo/update-redis-apt
34211ad54 Merge pull request #1063 from redis/fix-windows-tests
9957af7e3 Whitelist hiredis repo path in cygwin
b455b3381 Handle push notifications before or after reply.
aed9ce446 Use official repository for redis package.
d7683f35a Merge pull request #1047 from Nordix/unsubscribe-handling
7c44a9d7e Merge pull request #1045 from Nordix/sds-updates
dd4bf9783 Use the same name for static and shared libraries
ff57c18b9 Embed debug information in windows static lib, rather than create a .pdb file
8310ad4f5 fix cmake version
7123b87f6 Handle any pipelined unsubscribe in async
b6fb548fc Ignore pubsub replies without a channel/pattern
00b82683b Handle overflows as errors instead of asserting
64062a1d4 Catch size_t overflows in sds.c
066c6de79 Use size_t/long to avoid truncation
c6657ef65 Merge branch 'redis:master' into master
50cdcab49 Fix potential fault at createDoubleObject
fd033e983 Remove semicolon after do-while in _EL_CLEANUP
664c415e7 Illumos test fixes, error message difference fot bad hostname test.
git-subtree-dir: deps/hiredis
git-subtree-split: b6a052fe0959dae69e16b9d74449faeb1b70dbe1
2023-05-30 22:23:45 +03:00
|
|
|
long i;
|
2020-08-06 12:41:58 -07:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap,fmt);
|
2020-08-15 12:24:31 -07:00
|
|
|
i = hi_sdslen(s); /* Position of the next byte to write to dest str. */
|
2020-08-06 12:41:58 -07:00
|
|
|
while(*f) {
|
|
|
|
char next, *str;
|
|
|
|
size_t l;
|
|
|
|
long long num;
|
|
|
|
unsigned long long unum;
|
|
|
|
|
|
|
|
/* Make sure there is always space for at least 1 char. */
|
2020-08-15 12:24:31 -07:00
|
|
|
if (hi_sdsavail(s)==0) {
|
|
|
|
s = hi_sdsMakeRoomFor(s,1);
|
2020-08-06 12:41:58 -07:00
|
|
|
if (s == NULL) goto fmt_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(*f) {
|
|
|
|
case '%':
|
|
|
|
next = *(f+1);
|
|
|
|
f++;
|
|
|
|
switch(next) {
|
|
|
|
case 's':
|
|
|
|
case 'S':
|
|
|
|
str = va_arg(ap,char*);
|
2020-08-15 12:24:31 -07:00
|
|
|
l = (next == 's') ? strlen(str) : hi_sdslen(str);
|
|
|
|
if (hi_sdsavail(s) < l) {
|
|
|
|
s = hi_sdsMakeRoomFor(s,l);
|
2020-08-06 12:41:58 -07:00
|
|
|
if (s == NULL) goto fmt_error;
|
|
|
|
}
|
|
|
|
memcpy(s+i,str,l);
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdsinclen(s,l);
|
2020-08-06 12:41:58 -07:00
|
|
|
i += l;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
case 'I':
|
|
|
|
if (next == 'i')
|
|
|
|
num = va_arg(ap,int);
|
|
|
|
else
|
|
|
|
num = va_arg(ap,long long);
|
|
|
|
{
|
2020-08-15 12:24:31 -07:00
|
|
|
char buf[HI_SDS_LLSTR_SIZE];
|
|
|
|
l = hi_sdsll2str(buf,num);
|
|
|
|
if (hi_sdsavail(s) < l) {
|
|
|
|
s = hi_sdsMakeRoomFor(s,l);
|
2020-08-06 12:41:58 -07:00
|
|
|
if (s == NULL) goto fmt_error;
|
|
|
|
}
|
|
|
|
memcpy(s+i,buf,l);
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdsinclen(s,l);
|
2020-08-06 12:41:58 -07:00
|
|
|
i += l;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
case 'U':
|
|
|
|
if (next == 'u')
|
|
|
|
unum = va_arg(ap,unsigned int);
|
|
|
|
else
|
|
|
|
unum = va_arg(ap,unsigned long long);
|
|
|
|
{
|
2020-08-15 12:24:31 -07:00
|
|
|
char buf[HI_SDS_LLSTR_SIZE];
|
|
|
|
l = hi_sdsull2str(buf,unum);
|
|
|
|
if (hi_sdsavail(s) < l) {
|
|
|
|
s = hi_sdsMakeRoomFor(s,l);
|
2020-08-06 12:41:58 -07:00
|
|
|
if (s == NULL) goto fmt_error;
|
|
|
|
}
|
|
|
|
memcpy(s+i,buf,l);
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdsinclen(s,l);
|
2020-08-06 12:41:58 -07:00
|
|
|
i += l;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: /* Handle %% and generally %<unknown>. */
|
|
|
|
s[i++] = next;
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdsinclen(s,1);
|
2020-08-06 12:41:58 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
s[i++] = *f;
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdsinclen(s,1);
|
2020-08-06 12:41:58 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
f++;
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
/* Add null-term */
|
|
|
|
s[i] = '\0';
|
|
|
|
return s;
|
|
|
|
|
|
|
|
fmt_error:
|
|
|
|
va_end(ap);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the part of the string from left and from right composed just of
|
|
|
|
* contiguous characters found in 'cset', that is a null terminted C string.
|
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* After the call, the modified hisds string is no longer valid and all the
|
2020-08-06 12:41:58 -07:00
|
|
|
* references must be substituted with the new pointer returned by the call.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* s = hi_sdsnew("AA...AA.a.aa.aHelloWorld :::");
|
|
|
|
* s = hi_sdstrim(s,"Aa. :");
|
2020-08-06 12:41:58 -07:00
|
|
|
* printf("%s\n", s);
|
|
|
|
*
|
|
|
|
* Output will be just "Hello World".
|
|
|
|
*/
|
2020-08-15 12:24:31 -07:00
|
|
|
hisds hi_sdstrim(hisds s, const char *cset) {
|
2020-08-06 12:41:58 -07:00
|
|
|
char *start, *end, *sp, *ep;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
sp = start = s;
|
2020-08-15 12:24:31 -07:00
|
|
|
ep = end = s+hi_sdslen(s)-1;
|
2020-08-06 12:41:58 -07:00
|
|
|
while(sp <= end && strchr(cset, *sp)) sp++;
|
|
|
|
while(ep > sp && strchr(cset, *ep)) ep--;
|
|
|
|
len = (sp > ep) ? 0 : ((ep-sp)+1);
|
|
|
|
if (s != sp) memmove(s, sp, len);
|
|
|
|
s[len] = '\0';
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdssetlen(s,len);
|
2020-08-06 12:41:58 -07:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Turn the string into a smaller (or equal) string containing only the
|
|
|
|
* substring specified by the 'start' and 'end' indexes.
|
|
|
|
*
|
|
|
|
* start and end can be negative, where -1 means the last character of the
|
|
|
|
* string, -2 the penultimate character, and so forth.
|
|
|
|
*
|
|
|
|
* The interval is inclusive, so the start and end characters will be part
|
|
|
|
* of the resulting string.
|
|
|
|
*
|
|
|
|
* The string is modified in-place.
|
|
|
|
*
|
|
|
|
* Return value:
|
2020-08-15 12:24:31 -07:00
|
|
|
* -1 (error) if hi_sdslen(s) is larger than maximum positive ssize_t value.
|
2020-08-06 12:41:58 -07:00
|
|
|
* 0 on success.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* s = hi_sdsnew("Hello World");
|
|
|
|
* hi_sdsrange(s,1,-1); => "ello World"
|
2020-08-06 12:41:58 -07:00
|
|
|
*/
|
2020-08-15 12:24:31 -07:00
|
|
|
int hi_sdsrange(hisds s, ssize_t start, ssize_t end) {
|
|
|
|
size_t newlen, len = hi_sdslen(s);
|
2020-08-06 12:41:58 -07:00
|
|
|
if (len > SSIZE_MAX) return -1;
|
|
|
|
|
|
|
|
if (len == 0) return 0;
|
|
|
|
if (start < 0) {
|
|
|
|
start = len+start;
|
|
|
|
if (start < 0) start = 0;
|
|
|
|
}
|
|
|
|
if (end < 0) {
|
|
|
|
end = len+end;
|
|
|
|
if (end < 0) end = 0;
|
|
|
|
}
|
|
|
|
newlen = (start > end) ? 0 : (end-start)+1;
|
|
|
|
if (newlen != 0) {
|
|
|
|
if (start >= (ssize_t)len) {
|
|
|
|
newlen = 0;
|
|
|
|
} else if (end >= (ssize_t)len) {
|
|
|
|
end = len-1;
|
|
|
|
newlen = (start > end) ? 0 : (end-start)+1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
if (start && newlen) memmove(s, s+start, newlen);
|
|
|
|
s[newlen] = 0;
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdssetlen(s,newlen);
|
2020-08-06 12:41:58 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-14 13:51:42 +02:00
|
|
|
/* Apply tolower() to every character of the sds string 's'. */
|
2020-08-15 12:24:31 -07:00
|
|
|
void hi_sdstolower(hisds s) {
|
2023-05-30 22:28:26 +03:00
|
|
|
size_t len = hi_sdslen(s), j;
|
2020-08-06 12:41:58 -07:00
|
|
|
|
|
|
|
for (j = 0; j < len; j++) s[j] = tolower(s[j]);
|
|
|
|
}
|
|
|
|
|
2022-02-14 13:51:42 +02:00
|
|
|
/* Apply toupper() to every character of the sds string 's'. */
|
2020-08-15 12:24:31 -07:00
|
|
|
void hi_sdstoupper(hisds s) {
|
2023-05-30 22:28:26 +03:00
|
|
|
size_t len = hi_sdslen(s), j;
|
2020-08-06 12:41:58 -07:00
|
|
|
|
|
|
|
for (j = 0; j < len; j++) s[j] = toupper(s[j]);
|
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Compare two hisds strings s1 and s2 with memcmp().
|
2020-08-06 12:41:58 -07:00
|
|
|
*
|
|
|
|
* Return value:
|
|
|
|
*
|
|
|
|
* positive if s1 > s2.
|
|
|
|
* negative if s1 < s2.
|
|
|
|
* 0 if s1 and s2 are exactly the same binary string.
|
|
|
|
*
|
|
|
|
* If two strings share exactly the same prefix, but one of the two has
|
|
|
|
* additional characters, the longer string is considered to be greater than
|
|
|
|
* the smaller one. */
|
2020-08-15 12:24:31 -07:00
|
|
|
int hi_sdscmp(const hisds s1, const hisds s2) {
|
2020-08-06 12:41:58 -07:00
|
|
|
size_t l1, l2, minlen;
|
|
|
|
int cmp;
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
l1 = hi_sdslen(s1);
|
|
|
|
l2 = hi_sdslen(s2);
|
2020-08-06 12:41:58 -07:00
|
|
|
minlen = (l1 < l2) ? l1 : l2;
|
|
|
|
cmp = memcmp(s1,s2,minlen);
|
|
|
|
if (cmp == 0) return l1-l2;
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Split 's' with separator in 'sep'. An array
|
2020-08-15 12:24:31 -07:00
|
|
|
* of hisds strings is returned. *count will be set
|
2020-08-06 12:41:58 -07:00
|
|
|
* by reference to the number of tokens returned.
|
|
|
|
*
|
|
|
|
* On out of memory, zero length string, zero length
|
|
|
|
* separator, NULL is returned.
|
|
|
|
*
|
|
|
|
* Note that 'sep' is able to split a string using
|
|
|
|
* a multi-character separator. For example
|
2020-08-15 12:24:31 -07:00
|
|
|
* hi_sdssplit("foo_-_bar","_-_"); will return two
|
2020-08-06 12:41:58 -07:00
|
|
|
* elements "foo" and "bar".
|
|
|
|
*
|
|
|
|
* This version of the function is binary-safe but
|
2020-08-15 12:24:31 -07:00
|
|
|
* requires length arguments. hi_sdssplit() is just the
|
2020-08-06 12:41:58 -07:00
|
|
|
* same function but for zero-terminated strings.
|
|
|
|
*/
|
2020-08-15 12:24:31 -07:00
|
|
|
hisds *hi_sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count) {
|
2020-08-06 12:41:58 -07:00
|
|
|
int elements = 0, slots = 5, start = 0, j;
|
2020-08-15 12:24:31 -07:00
|
|
|
hisds *tokens;
|
2020-08-06 12:41:58 -07:00
|
|
|
|
|
|
|
if (seplen < 1 || len < 0) return NULL;
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
tokens = hi_s_malloc(sizeof(hisds)*slots);
|
2020-08-06 12:41:58 -07:00
|
|
|
if (tokens == NULL) return NULL;
|
|
|
|
|
|
|
|
if (len == 0) {
|
|
|
|
*count = 0;
|
|
|
|
return tokens;
|
|
|
|
}
|
|
|
|
for (j = 0; j < (len-(seplen-1)); j++) {
|
|
|
|
/* make sure there is room for the next element and the final one */
|
|
|
|
if (slots < elements+2) {
|
2020-08-15 12:24:31 -07:00
|
|
|
hisds *newtokens;
|
2020-08-06 12:41:58 -07:00
|
|
|
|
|
|
|
slots *= 2;
|
2020-08-15 12:24:31 -07:00
|
|
|
newtokens = hi_s_realloc(tokens,sizeof(hisds)*slots);
|
2020-08-06 12:41:58 -07:00
|
|
|
if (newtokens == NULL) goto cleanup;
|
|
|
|
tokens = newtokens;
|
|
|
|
}
|
|
|
|
/* search the separator */
|
|
|
|
if ((seplen == 1 && *(s+j) == sep[0]) || (memcmp(s+j,sep,seplen) == 0)) {
|
2020-08-15 12:24:31 -07:00
|
|
|
tokens[elements] = hi_sdsnewlen(s+start,j-start);
|
2020-08-06 12:41:58 -07:00
|
|
|
if (tokens[elements] == NULL) goto cleanup;
|
|
|
|
elements++;
|
|
|
|
start = j+seplen;
|
|
|
|
j = j+seplen-1; /* skip the separator */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Add the final element. We are sure there is room in the tokens array. */
|
2020-08-15 12:24:31 -07:00
|
|
|
tokens[elements] = hi_sdsnewlen(s+start,len-start);
|
2020-08-06 12:41:58 -07:00
|
|
|
if (tokens[elements] == NULL) goto cleanup;
|
|
|
|
elements++;
|
|
|
|
*count = elements;
|
|
|
|
return tokens;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
{
|
|
|
|
int i;
|
2020-08-15 12:24:31 -07:00
|
|
|
for (i = 0; i < elements; i++) hi_sdsfree(tokens[i]);
|
|
|
|
hi_s_free(tokens);
|
2020-08-06 12:41:58 -07:00
|
|
|
*count = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Free the result returned by hi_sdssplitlen(), or do nothing if 'tokens' is NULL. */
|
|
|
|
void hi_sdsfreesplitres(hisds *tokens, int count) {
|
2020-08-06 12:41:58 -07:00
|
|
|
if (!tokens) return;
|
|
|
|
while(count--)
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdsfree(tokens[count]);
|
|
|
|
hi_s_free(tokens);
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Append to the hisds string "s" an escaped string representation where
|
2020-08-06 12:41:58 -07:00
|
|
|
* all the non-printable characters (tested with isprint()) are turned into
|
|
|
|
* escapes in the form "\n\r\a...." or "\x<hex-number>".
|
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* After the call, the modified hisds string is no longer valid and all the
|
2020-08-06 12:41:58 -07:00
|
|
|
* references must be substituted with the new pointer returned by the call. */
|
2020-08-15 12:24:31 -07:00
|
|
|
hisds hi_sdscatrepr(hisds s, const char *p, size_t len) {
|
|
|
|
s = hi_sdscatlen(s,"\"",1);
|
2020-08-06 12:41:58 -07:00
|
|
|
while(len--) {
|
|
|
|
switch(*p) {
|
|
|
|
case '\\':
|
|
|
|
case '"':
|
2020-08-15 12:24:31 -07:00
|
|
|
s = hi_sdscatprintf(s,"\\%c",*p);
|
2020-08-06 12:41:58 -07:00
|
|
|
break;
|
2020-08-15 12:24:31 -07:00
|
|
|
case '\n': s = hi_sdscatlen(s,"\\n",2); break;
|
|
|
|
case '\r': s = hi_sdscatlen(s,"\\r",2); break;
|
|
|
|
case '\t': s = hi_sdscatlen(s,"\\t",2); break;
|
|
|
|
case '\a': s = hi_sdscatlen(s,"\\a",2); break;
|
|
|
|
case '\b': s = hi_sdscatlen(s,"\\b",2); break;
|
2020-08-06 12:41:58 -07:00
|
|
|
default:
|
|
|
|
if (isprint(*p))
|
2020-08-15 12:24:31 -07:00
|
|
|
s = hi_sdscatprintf(s,"%c",*p);
|
2020-08-06 12:41:58 -07:00
|
|
|
else
|
2020-08-15 12:24:31 -07:00
|
|
|
s = hi_sdscatprintf(s,"\\x%02x",(unsigned char)*p);
|
2020-08-06 12:41:58 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
2020-08-15 12:24:31 -07:00
|
|
|
return hi_sdscatlen(s,"\"",1);
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Helper function for hi_sdssplitargs() that converts a hex digit into an
|
2020-08-06 12:41:58 -07:00
|
|
|
* integer from 0 to 15 */
|
2020-08-15 12:24:31 -07:00
|
|
|
static int hi_hex_digit_to_int(char c) {
|
2020-08-06 12:41:58 -07:00
|
|
|
switch(c) {
|
|
|
|
case '0': return 0;
|
|
|
|
case '1': return 1;
|
|
|
|
case '2': return 2;
|
|
|
|
case '3': return 3;
|
|
|
|
case '4': return 4;
|
|
|
|
case '5': return 5;
|
|
|
|
case '6': return 6;
|
|
|
|
case '7': return 7;
|
|
|
|
case '8': return 8;
|
|
|
|
case '9': return 9;
|
|
|
|
case 'a': case 'A': return 10;
|
|
|
|
case 'b': case 'B': return 11;
|
|
|
|
case 'c': case 'C': return 12;
|
|
|
|
case 'd': case 'D': return 13;
|
|
|
|
case 'e': case 'E': return 14;
|
|
|
|
case 'f': case 'F': return 15;
|
|
|
|
default: return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Split a line into arguments, where every argument can be in the
|
|
|
|
* following programming-language REPL-alike form:
|
|
|
|
*
|
|
|
|
* foo bar "newline are supported\n" and "\xff\x00otherstuff"
|
|
|
|
*
|
|
|
|
* The number of arguments is stored into *argc, and an array
|
2020-08-15 12:24:31 -07:00
|
|
|
* of hisds is returned.
|
2020-08-06 12:41:58 -07:00
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* The caller should free the resulting array of hisds strings with
|
|
|
|
* hi_sdsfreesplitres().
|
2020-08-06 12:41:58 -07:00
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* Note that hi_sdscatrepr() is able to convert back a string into
|
|
|
|
* a quoted string in the same format hi_sdssplitargs() is able to parse.
|
2020-08-06 12:41:58 -07:00
|
|
|
*
|
|
|
|
* The function returns the allocated tokens on success, even when the
|
|
|
|
* input string is empty, or NULL if the input contains unbalanced
|
|
|
|
* quotes or closed quotes followed by non space characters
|
|
|
|
* as in: "foo"bar or "foo'
|
|
|
|
*/
|
2020-08-15 12:24:31 -07:00
|
|
|
hisds *hi_sdssplitargs(const char *line, int *argc) {
|
2020-08-06 12:41:58 -07:00
|
|
|
const char *p = line;
|
|
|
|
char *current = NULL;
|
|
|
|
char **vector = NULL;
|
|
|
|
|
|
|
|
*argc = 0;
|
|
|
|
while(1) {
|
|
|
|
/* skip blanks */
|
2023-07-13 09:23:10 +03:00
|
|
|
while(*p && isspace((int) *p)) p++;
|
2020-08-06 12:41:58 -07:00
|
|
|
if (*p) {
|
|
|
|
/* get a token */
|
|
|
|
int inq=0; /* set to 1 if we are in "quotes" */
|
|
|
|
int insq=0; /* set to 1 if we are in 'single quotes' */
|
|
|
|
int done=0;
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
if (current == NULL) current = hi_sdsempty();
|
2020-08-06 12:41:58 -07:00
|
|
|
while(!done) {
|
|
|
|
if (inq) {
|
|
|
|
if (*p == '\\' && *(p+1) == 'x' &&
|
2023-07-13 09:23:10 +03:00
|
|
|
isxdigit((int) *(p+2)) &&
|
|
|
|
isxdigit((int) *(p+3)))
|
2020-08-06 12:41:58 -07:00
|
|
|
{
|
|
|
|
unsigned char byte;
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
byte = (hi_hex_digit_to_int(*(p+2))*16)+
|
|
|
|
hi_hex_digit_to_int(*(p+3));
|
|
|
|
current = hi_sdscatlen(current,(char*)&byte,1);
|
2020-08-06 12:41:58 -07:00
|
|
|
p += 3;
|
|
|
|
} else if (*p == '\\' && *(p+1)) {
|
|
|
|
char c;
|
|
|
|
|
|
|
|
p++;
|
|
|
|
switch(*p) {
|
|
|
|
case 'n': c = '\n'; break;
|
|
|
|
case 'r': c = '\r'; break;
|
|
|
|
case 't': c = '\t'; break;
|
|
|
|
case 'b': c = '\b'; break;
|
|
|
|
case 'a': c = '\a'; break;
|
|
|
|
default: c = *p; break;
|
|
|
|
}
|
2020-08-15 12:24:31 -07:00
|
|
|
current = hi_sdscatlen(current,&c,1);
|
2020-08-06 12:41:58 -07:00
|
|
|
} else if (*p == '"') {
|
|
|
|
/* closing quote must be followed by a space or
|
|
|
|
* nothing at all. */
|
2023-07-13 09:23:10 +03:00
|
|
|
if (*(p+1) && !isspace((int) *(p+1))) goto err;
|
2020-08-06 12:41:58 -07:00
|
|
|
done=1;
|
|
|
|
} else if (!*p) {
|
|
|
|
/* unterminated quotes */
|
|
|
|
goto err;
|
|
|
|
} else {
|
2020-08-15 12:24:31 -07:00
|
|
|
current = hi_sdscatlen(current,p,1);
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
} else if (insq) {
|
|
|
|
if (*p == '\\' && *(p+1) == '\'') {
|
|
|
|
p++;
|
2020-08-15 12:24:31 -07:00
|
|
|
current = hi_sdscatlen(current,"'",1);
|
2020-08-06 12:41:58 -07:00
|
|
|
} else if (*p == '\'') {
|
|
|
|
/* closing quote must be followed by a space or
|
|
|
|
* nothing at all. */
|
2023-07-13 09:23:10 +03:00
|
|
|
if (*(p+1) && !isspace((int) *(p+1))) goto err;
|
2020-08-06 12:41:58 -07:00
|
|
|
done=1;
|
|
|
|
} else if (!*p) {
|
|
|
|
/* unterminated quotes */
|
|
|
|
goto err;
|
|
|
|
} else {
|
2020-08-15 12:24:31 -07:00
|
|
|
current = hi_sdscatlen(current,p,1);
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch(*p) {
|
|
|
|
case ' ':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
case '\t':
|
|
|
|
case '\0':
|
|
|
|
done=1;
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
inq=1;
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
insq=1;
|
|
|
|
break;
|
|
|
|
default:
|
2020-08-15 12:24:31 -07:00
|
|
|
current = hi_sdscatlen(current,p,1);
|
2020-08-06 12:41:58 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*p) p++;
|
|
|
|
}
|
|
|
|
/* add the token to the vector */
|
|
|
|
{
|
2020-08-15 12:24:31 -07:00
|
|
|
char **new_vector = hi_s_realloc(vector,((*argc)+1)*sizeof(char*));
|
2020-08-06 12:41:58 -07:00
|
|
|
if (new_vector == NULL) {
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_s_free(vector);
|
2020-08-06 12:41:58 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector = new_vector;
|
|
|
|
vector[*argc] = current;
|
|
|
|
(*argc)++;
|
|
|
|
current = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Even on empty input string return something not NULL. */
|
2020-08-15 12:24:31 -07:00
|
|
|
if (vector == NULL) vector = hi_s_malloc(sizeof(void*));
|
2020-08-06 12:41:58 -07:00
|
|
|
return vector;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
|
|
|
while((*argc)--)
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdsfree(vector[*argc]);
|
|
|
|
hi_s_free(vector);
|
|
|
|
if (current) hi_sdsfree(current);
|
2020-08-06 12:41:58 -07:00
|
|
|
*argc = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Modify the string substituting all the occurrences of the set of
|
|
|
|
* characters specified in the 'from' string to the corresponding character
|
|
|
|
* in the 'to' array.
|
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* For instance: hi_sdsmapchars(mystring, "ho", "01", 2)
|
2020-08-06 12:41:58 -07:00
|
|
|
* will have the effect of turning the string "hello" into "0ell1".
|
|
|
|
*
|
2020-08-15 12:24:31 -07:00
|
|
|
* The function returns the hisds string pointer, that is always the same
|
2020-08-06 12:41:58 -07:00
|
|
|
* as the input pointer since no resize is needed. */
|
2020-08-15 12:24:31 -07:00
|
|
|
hisds hi_sdsmapchars(hisds s, const char *from, const char *to, size_t setlen) {
|
|
|
|
size_t j, i, l = hi_sdslen(s);
|
2020-08-06 12:41:58 -07:00
|
|
|
|
|
|
|
for (j = 0; j < l; j++) {
|
|
|
|
for (i = 0; i < setlen; i++) {
|
|
|
|
if (s[j] == from[i]) {
|
|
|
|
s[j] = to[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Join an array of C strings using the specified separator (also a C string).
|
2020-08-15 12:24:31 -07:00
|
|
|
* Returns the result as an hisds string. */
|
|
|
|
hisds hi_sdsjoin(char **argv, int argc, char *sep) {
|
|
|
|
hisds join = hi_sdsempty();
|
2020-08-06 12:41:58 -07:00
|
|
|
int j;
|
|
|
|
|
|
|
|
for (j = 0; j < argc; j++) {
|
2020-08-15 12:24:31 -07:00
|
|
|
join = hi_sdscat(join, argv[j]);
|
|
|
|
if (j != argc-1) join = hi_sdscat(join,sep);
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
return join;
|
|
|
|
}
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
/* Like hi_sdsjoin, but joins an array of SDS strings. */
|
|
|
|
hisds hi_sdsjoinsds(hisds *argv, int argc, const char *sep, size_t seplen) {
|
|
|
|
hisds join = hi_sdsempty();
|
2020-08-06 12:41:58 -07:00
|
|
|
int j;
|
|
|
|
|
|
|
|
for (j = 0; j < argc; j++) {
|
2020-08-15 12:24:31 -07:00
|
|
|
join = hi_sdscatsds(join, argv[j]);
|
|
|
|
if (j != argc-1) join = hi_sdscatlen(join,sep,seplen);
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
return join;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wrappers to the allocators used by SDS. Note that SDS will actually
|
|
|
|
* just use the macros defined into sdsalloc.h in order to avoid to pay
|
|
|
|
* the overhead of function calls. Here we define these wrappers only for
|
|
|
|
* the programs SDS is linked to, if they want to touch the SDS internals
|
|
|
|
* even if they use a different allocator. */
|
2020-08-15 12:24:31 -07:00
|
|
|
void *hi_sds_malloc(size_t size) { return hi_s_malloc(size); }
|
|
|
|
void *hi_sds_realloc(void *ptr, size_t size) { return hi_s_realloc(ptr,size); }
|
|
|
|
void hi_sds_free(void *ptr) { hi_s_free(ptr); }
|
2020-08-06 12:41:58 -07:00
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
#if defined(HI_SDS_TEST_MAIN)
|
2020-08-06 12:41:58 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "testhelp.h"
|
|
|
|
#include "limits.h"
|
|
|
|
|
|
|
|
#define UNUSED(x) (void)(x)
|
2020-08-15 12:24:31 -07:00
|
|
|
int hi_sdsTest(void) {
|
2020-08-06 12:41:58 -07:00
|
|
|
{
|
2020-08-15 12:24:31 -07:00
|
|
|
hisds x = hi_sdsnew("foo"), y;
|
2020-08-06 12:41:58 -07:00
|
|
|
|
|
|
|
test_cond("Create a string and obtain the length",
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdslen(x) == 3 && memcmp(x,"foo\0",4) == 0)
|
2020-08-06 12:41:58 -07:00
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdsfree(x);
|
|
|
|
x = hi_sdsnewlen("foo",2);
|
2020-08-06 12:41:58 -07:00
|
|
|
test_cond("Create a string with specified length",
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdslen(x) == 2 && memcmp(x,"fo\0",3) == 0)
|
2020-08-06 12:41:58 -07:00
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
x = hi_sdscat(x,"bar");
|
2020-08-06 12:41:58 -07:00
|
|
|
test_cond("Strings concatenation",
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdslen(x) == 5 && memcmp(x,"fobar\0",6) == 0);
|
2020-08-06 12:41:58 -07:00
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
x = hi_sdscpy(x,"a");
|
|
|
|
test_cond("hi_sdscpy() against an originally longer string",
|
|
|
|
hi_sdslen(x) == 1 && memcmp(x,"a\0",2) == 0)
|
2020-08-06 12:41:58 -07:00
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
x = hi_sdscpy(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk");
|
|
|
|
test_cond("hi_sdscpy() against an originally shorter string",
|
|
|
|
hi_sdslen(x) == 33 &&
|
2020-08-06 12:41:58 -07:00
|
|
|
memcmp(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0)
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdsfree(x);
|
|
|
|
x = hi_sdscatprintf(hi_sdsempty(),"%d",123);
|
|
|
|
test_cond("hi_sdscatprintf() seems working in the base case",
|
|
|
|
hi_sdslen(x) == 3 && memcmp(x,"123\0",4) == 0)
|
2020-08-06 12:41:58 -07:00
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdsfree(x);
|
|
|
|
x = hi_sdsnew("--");
|
|
|
|
x = hi_sdscatfmt(x, "Hello %s World %I,%I--", "Hi!", LLONG_MIN,LLONG_MAX);
|
|
|
|
test_cond("hi_sdscatfmt() seems working in the base case",
|
|
|
|
hi_sdslen(x) == 60 &&
|
2020-08-06 12:41:58 -07:00
|
|
|
memcmp(x,"--Hello Hi! World -9223372036854775808,"
|
|
|
|
"9223372036854775807--",60) == 0)
|
|
|
|
printf("[%s]\n",x);
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdsfree(x);
|
|
|
|
x = hi_sdsnew("--");
|
|
|
|
x = hi_sdscatfmt(x, "%u,%U--", UINT_MAX, ULLONG_MAX);
|
|
|
|
test_cond("hi_sdscatfmt() seems working with unsigned numbers",
|
|
|
|
hi_sdslen(x) == 35 &&
|
2020-08-06 12:41:58 -07:00
|
|
|
memcmp(x,"--4294967295,18446744073709551615--",35) == 0)
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdsfree(x);
|
|
|
|
x = hi_sdsnew(" x ");
|
|
|
|
hi_sdstrim(x," x");
|
|
|
|
test_cond("hi_sdstrim() works when all chars match",
|
|
|
|
hi_sdslen(x) == 0)
|
|
|
|
|
|
|
|
hi_sdsfree(x);
|
|
|
|
x = hi_sdsnew(" x ");
|
|
|
|
hi_sdstrim(x," ");
|
|
|
|
test_cond("hi_sdstrim() works when a single char remains",
|
|
|
|
hi_sdslen(x) == 1 && x[0] == 'x')
|
|
|
|
|
|
|
|
hi_sdsfree(x);
|
|
|
|
x = hi_sdsnew("xxciaoyyy");
|
|
|
|
hi_sdstrim(x,"xy");
|
|
|
|
test_cond("hi_sdstrim() correctly trims characters",
|
|
|
|
hi_sdslen(x) == 4 && memcmp(x,"ciao\0",5) == 0)
|
|
|
|
|
|
|
|
y = hi_sdsdup(x);
|
|
|
|
hi_sdsrange(y,1,1);
|
|
|
|
test_cond("hi_sdsrange(...,1,1)",
|
|
|
|
hi_sdslen(y) == 1 && memcmp(y,"i\0",2) == 0)
|
|
|
|
|
|
|
|
hi_sdsfree(y);
|
|
|
|
y = hi_sdsdup(x);
|
|
|
|
hi_sdsrange(y,1,-1);
|
|
|
|
test_cond("hi_sdsrange(...,1,-1)",
|
|
|
|
hi_sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)
|
|
|
|
|
|
|
|
hi_sdsfree(y);
|
|
|
|
y = hi_sdsdup(x);
|
|
|
|
hi_sdsrange(y,-2,-1);
|
|
|
|
test_cond("hi_sdsrange(...,-2,-1)",
|
|
|
|
hi_sdslen(y) == 2 && memcmp(y,"ao\0",3) == 0)
|
|
|
|
|
|
|
|
hi_sdsfree(y);
|
|
|
|
y = hi_sdsdup(x);
|
|
|
|
hi_sdsrange(y,2,1);
|
|
|
|
test_cond("hi_sdsrange(...,2,1)",
|
|
|
|
hi_sdslen(y) == 0 && memcmp(y,"\0",1) == 0)
|
|
|
|
|
|
|
|
hi_sdsfree(y);
|
|
|
|
y = hi_sdsdup(x);
|
|
|
|
hi_sdsrange(y,1,100);
|
|
|
|
test_cond("hi_sdsrange(...,1,100)",
|
|
|
|
hi_sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)
|
|
|
|
|
|
|
|
hi_sdsfree(y);
|
|
|
|
y = hi_sdsdup(x);
|
|
|
|
hi_sdsrange(y,100,100);
|
|
|
|
test_cond("hi_sdsrange(...,100,100)",
|
|
|
|
hi_sdslen(y) == 0 && memcmp(y,"\0",1) == 0)
|
|
|
|
|
|
|
|
hi_sdsfree(y);
|
|
|
|
hi_sdsfree(x);
|
|
|
|
x = hi_sdsnew("foo");
|
|
|
|
y = hi_sdsnew("foa");
|
|
|
|
test_cond("hi_sdscmp(foo,foa)", hi_sdscmp(x,y) > 0)
|
|
|
|
|
|
|
|
hi_sdsfree(y);
|
|
|
|
hi_sdsfree(x);
|
|
|
|
x = hi_sdsnew("bar");
|
|
|
|
y = hi_sdsnew("bar");
|
|
|
|
test_cond("hi_sdscmp(bar,bar)", hi_sdscmp(x,y) == 0)
|
|
|
|
|
|
|
|
hi_sdsfree(y);
|
|
|
|
hi_sdsfree(x);
|
|
|
|
x = hi_sdsnew("aar");
|
|
|
|
y = hi_sdsnew("bar");
|
|
|
|
test_cond("hi_sdscmp(bar,bar)", hi_sdscmp(x,y) < 0)
|
|
|
|
|
|
|
|
hi_sdsfree(y);
|
|
|
|
hi_sdsfree(x);
|
|
|
|
x = hi_sdsnewlen("\a\n\0foo\r",7);
|
|
|
|
y = hi_sdscatrepr(hi_sdsempty(),x,hi_sdslen(x));
|
|
|
|
test_cond("hi_sdscatrepr(...data...)",
|
2020-08-06 12:41:58 -07:00
|
|
|
memcmp(y,"\"\\a\\n\\x00foo\\r\"",15) == 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
unsigned int oldfree;
|
|
|
|
char *p;
|
|
|
|
int step = 10, j, i;
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdsfree(x);
|
|
|
|
hi_sdsfree(y);
|
|
|
|
x = hi_sdsnew("0");
|
|
|
|
test_cond("hi_sdsnew() free/len buffers", hi_sdslen(x) == 1 && hi_sdsavail(x) == 0);
|
2020-08-06 12:41:58 -07:00
|
|
|
|
|
|
|
/* Run the test a few times in order to hit the first two
|
|
|
|
* SDS header types. */
|
|
|
|
for (i = 0; i < 10; i++) {
|
2020-08-15 12:24:31 -07:00
|
|
|
int oldlen = hi_sdslen(x);
|
|
|
|
x = hi_sdsMakeRoomFor(x,step);
|
|
|
|
int type = x[-1]&HI_SDS_TYPE_MASK;
|
|
|
|
|
|
|
|
test_cond("sdsMakeRoomFor() len", hi_sdslen(x) == oldlen);
|
|
|
|
if (type != HI_SDS_TYPE_5) {
|
|
|
|
test_cond("hi_sdsMakeRoomFor() free", hi_sdsavail(x) >= step);
|
|
|
|
oldfree = hi_sdsavail(x);
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
p = x+oldlen;
|
|
|
|
for (j = 0; j < step; j++) {
|
|
|
|
p[j] = 'A'+j;
|
|
|
|
}
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdsIncrLen(x,step);
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
2020-08-15 12:24:31 -07:00
|
|
|
test_cond("hi_sdsMakeRoomFor() content",
|
2020-08-06 12:41:58 -07:00
|
|
|
memcmp("0ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ",x,101) == 0);
|
2020-08-15 12:24:31 -07:00
|
|
|
test_cond("sdsMakeRoomFor() final length",hi_sdslen(x)==101);
|
2020-08-06 12:41:58 -07:00
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
hi_sdsfree(x);
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
}
|
2020-08-15 12:24:31 -07:00
|
|
|
test_report();
|
2020-08-06 12:41:58 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-08-15 12:24:31 -07:00
|
|
|
#ifdef HI_SDS_TEST_MAIN
|
2020-08-06 12:41:58 -07:00
|
|
|
int main(void) {
|
2020-08-15 12:24:31 -07:00
|
|
|
return hi_sdsTest();
|
2020-08-06 12:41:58 -07:00
|
|
|
}
|
|
|
|
#endif
|