Commit Graph

190 Commits

Author SHA1 Message Date
Michael Grunder
d8ff72387d
Move SSL management to a distinct private pointer. (#855)
We need to allow our users to use redisContext->privdata as context
for any RESP3 PUSH messages, which means we can't use it for managing
SSL connections.

Bulletpoints:

* Create a secondary redisContext member for internal use only called
  privctx and rename the redisContextFuncs->free_privdata accordingly.

* Adds a `free_privdata` function pointer so the user can tie allocated
  memory to the lifetime of a redisContext (like they can already do
  with redisAsyncContext)

* Enables SSL tests in .travis.yml
2020-07-29 11:53:03 -07:00
valentinogeron
38b5ae543f
add a command_timeout to redisContextOptions (#839)
Add an additional timeout so the user has a convenient way of controlling distinct connect and command timeouts
2020-07-26 12:32:27 -07:00
Michael Grunder
2e7d7cbabd
Resp3 oob push support (#841)
Proper support for RESP3 PUSH messages.

By default, PUSH messages are now intercepted and the reply memory freed.  
This means existing code should work unchanged when connecting to Redis
>= 6.0.0 even if `CLIENT TRACKING` were then enabled.

Additionally, we define two callbacks users can configure if they wish to handle
these messages in a custom way:

void redisPushFn(void *privdata, void *reply);
void redisAsyncPushFn(redisAsyncContext *ac, void *reply);

See #825
2020-07-19 18:54:42 -07:00
valentinogeron
a28de70a01
timeout option in redisConnectWithOptions should be on connect only (#829)
When connecting with a timeout, we shouldn't also call `redisSetTimeout` which will implement a timeout for commands.

See related issue #722
2020-06-18 21:45:25 -07:00
Michael Grunder
6448f735d5
sdsrange overflow fix (#830)
Fix overflow bug in `sdsrange`
2020-06-07 14:38:16 -07:00
Michael Grunder
8e0264cfd6
Allow users to replace allocator and handle OOM everywhere. (#800)
* Adds an indirection to every allocation/deallocation to allow users to 
  plug in ones of their choosing (use custom functions, jemalloc, etc).

* Gracefully handle OOM everywhere in hiredis.  This should make it possible
  for users of the library to have more flexibility in how they handle such situations.

* Changes `redisReaderTask->elements` from an `int` to a `long long` to prevent
  a possible overflow when transferring the task elements into a `redisReply`.

* Adds a configurable `max elements` member to `redisReader` that defaults to
  2^32 - 1.  This can be set to "unlimited" by setting the value to zero.
2020-05-22 09:27:49 -07:00
Michael Grunder
83bba659b9
Add logic to handle RESP3 push messages (#819)
Fixes #815
2020-05-21 11:12:18 -07:00
Michael Grunder
5c9f49e212
Resp3 verbatim string support (#805)
Pull RESP3 verbatim string handling from Redis

Fixes #802
2020-05-19 12:56:02 -07:00
michael-grunder
ac0b186aa3 Free the reply in redisGetReply when passed NULL
We currently perform a NULL check in redisGetReply and don't push the
reply back to the caller, but we don't free any reply meaning that this
will leak memory:

redisGetReply(context, NULL);

This change simply frees the reply if we were passed NULL.

Addresses #740
2019-12-12 14:40:50 -08:00
Yossi Gottlieb
d41443bd3d Fix: redisReconnect() should clear SSL context.
We should not attempt to keep the context and re-establish the
TLS connection for several reasons:

1. Maintain symmetry between redisConnect() and redisReconnect(), so in
both cases an extra step is required to initiate SSL.
2. The caller may also wish to reconfigure the SSL session and needs a
chance to do that.
3. It is not a practical thing to do on an async non-blocking connection
context.
2019-09-16 17:30:35 +03:00
Yossi Gottlieb
8715ba5c82 wip: SSL code reorganization, see #705. 2019-08-29 22:09:37 +03:00
Mark Nunberg
ff4fa45422
Merge pull request #697 from yossigo/resp3
Port RESP3 support from Redis.
2019-08-27 06:59:32 -04:00
Mark Nunberg
f9bccfb7ba
Merge branch 'master' into createArray-size_t 2019-08-09 04:02:53 -04:00
Yossi Gottlieb
91de9c975a RESP3 support changes from Redis.
This corresponds to commits d5c54f0b..bea09a7f in the redis repository.
2019-08-04 12:13:04 +03:00
Mark Nunberg
4830786c84 ensure that blocking timeout is set 2019-04-10 08:36:06 -04:00
m
dc6d19b9ec Port network layer to Winsock
With this change, Hiredis builds with MinGW and runs on Windows.
2019-04-01 11:54:37 +02:00
m
e84086cb92 Introduce a redisFD type
The redisFD type should be equal to the system native socket file
desciptor type (for POSIX, this is a plain int).

We also introduce the REDIS_INVALID_FD value, which maps to -1 on POSIX
systems.
2019-04-01 11:52:24 +02:00
m
1788f41f16 Move network I/O calls to net.c
This makes hiredis.c free from system calls related to socket I/O. This
is also makes the treatment of raw socket connections more similar to
the SSL backend.
2019-04-01 11:52:24 +02:00
Mark Nunberg
5eb6958870 Allow option for async connections to not automatically free 2019-02-21 11:49:25 -05:00
Mark Nunberg
ea9f9d2515 fixed wrong memset args 2019-02-20 09:11:10 -05:00
Mark Nunberg
1eb44cf0c4 scrub redisContext before freeing
This helps us detect use-after-free
2019-02-20 09:11:10 -05:00
valentino
847a20122f Fix memory leaks 2019-02-20 09:10:10 -05:00
valentino
58222c26f4 Support SNI 2019-02-20 09:10:10 -05:00
Mark Nunberg
f4f6b6d65c minor fixes: initialize options struct with 0 always
also, clean up redisContextInit -- we're just zeoring the struct
2019-02-20 09:10:10 -05:00
Mark Nunberg
e4a78006e7 Provide option-struct initialization
This reduces the boilerplate of all the redisConnectXXX functions, and
allows us to provide more connection options in the future.
2019-02-20 09:10:10 -05:00
Mark Nunberg
0c14544906 Initial SSL (sync) implementation 2019-02-20 09:10:10 -05:00
Stefan Hacker
169fcc708b Fix redisBufferRead documentation
Referred to redisContextReadReply which I cannot find in this codebase
nor the old redis-tools one. Presumably this meant to say
redisGetReplyFromReader which is how redisBufferRead is used in this
file. Could've also meant the interface function redisReaderGetReply.
2018-10-24 23:56:51 +02:00
Mark Nunberg
49974c9359 Call connect(2) again for non-blocking connect
This retrieves the actual error which occurred, as getsockopt is not
always reliable in this regard.
2018-09-25 20:21:37 -04:00
Justin Brewer
ef4256670f Update createArray to take size_t
This makes createArray consistent with createString, which also takes
size_t. Bounds-check and unit tests are updated to allow up to
min(SIZE_MAX,LLONG_MAX).

Changelog is updated to mention this API break.

Signed-off-by: Justin Brewer <jzb0012@auburn.edu>
2018-05-21 10:49:30 -05:00
Justin Brewer
58e6b87f51 Remove redundant NULL checks
free(NULL) is a valid NOP. Most of the hiredis free functions behave the
same way. redisReaderFree is updated to also be NULL-safe.

There is one redundant NULL check at sds.c:1036, but it's left as is
since sds is imported from upstream.

Signed-off-by: Justin Brewer <jzb0012@auburn.edu>
2018-04-30 21:45:13 -05:00
Justin Brewer
54acc8f087 Remove redundant zero stores
calloc is guaranteed to provide a zero-initialized buffer. There is
no need to set fields to zero explicitly.

Signed-off-by: Justin Brewer <jzb0012@auburn.edu>
2018-04-30 21:45:13 -05:00
Justin Brewer
d1c1b668c1 Drop __redis_strerror_r
Since _GNU_SOURCE is now guaranteed to be unset, it is no longer
necessary to support the GNU-specific version of strerror_r.

Drop __redis_strerror_r from the header, and call strerror_r directly.
This breaks any external users of this macro, but they shouldn't have
been using it in  the first place.

Signed-off-by: Justin Brewer <jzb0012@auburn.edu>
2018-04-30 21:45:13 -05:00
cdliuqiang@gmail.com
9219556386 calloc param fixes and NULL check 2018-01-05 16:19:54 +01:00
amallia
3c32344a41 Small fixes 2017-06-15 20:59:37 +01:00
Paulino Huerta
360a0646bb Typo format in redisFormatSdsCommandArgv function
this issue is very significant, because not allow the proper execution of the "function redisCommandArgv". The server returns "invalid bulk length".
Thanks!
2016-05-14 01:53:48 +02:00
charsyam
e2f6ee239c fixing typos 2015-10-28 02:19:24 +09:00
Jan-Erik Rediger
d323b5ef9c Revert "redisBufferRead: Clear REDIS_CONNECTED flag when server closed connection"
This reverts commit 1db17f257b.

If the `REDIS_CONNECTED` flag is cleared,
the async onDisconnect callback function will never be called.
This causes problems as the disconnect is never reported back to the user.

Closes #359
2015-09-14 14:58:05 +02:00
Jerry Jacobs
1db17f257b redisBufferRead: Clear REDIS_CONNECTED flag when server closed connection 2015-08-24 17:03:16 +02:00
Alex Balashov
d132d676e9 Renamed redisContext struct member 'unix' to 'unix_sock' to avoid encountering defined constant 'unix' in GNU C environment (see commit d8145d79ce).
Not all code using hiredis can compile using '-std=c99', and/or not all users are able to easily make that change to the build process of various open-source projects, so it is more pragmatic to choose a different identifier that does not impose this requirement.
2015-04-30 15:01:31 -04:00
Jan-Erik Rediger
af598dbce5 Change copyright date and add copyright holder 2015-04-16 21:29:41 +02:00
Jan-Erik Rediger
d9e0b0f6ab Implement a reconnect method for the client context
Originally implemented by @abedra as part of #306.

In case a write or read times out, we force an error state, because we
can't guarantuee that the next read will get the right data.
Instead we need to reconnect to have a clean-state connection, which is
now easily possible with this method.
2015-04-16 21:00:30 +02:00
Matt Stancliff
195aca3427 Improve digit counting for multibulk creation
This replaces the old intlen() implementation with a slightly
faster way of counting digits.

Implementation taken from the same place where digits10() in
redis/src/util.c came from.

The old 'intlen' allowed negative inputs, but no usage in hiredis
was passing negative numbers, so that ability is removed.  Also,
the new implementation can count higher (uint64_t) instead of
limited to just int as before.

Fixes #295 by replacing implementation
2015-01-22 16:00:38 -05:00
Matt Stancliff
f28872ca70 Cleanup tabs and end of line whitespace 2015-01-05 16:53:23 -05:00
Matt Stancliff
cc20232406 Fix errno error buffers to not clobber errors
The strerror_r API has two flavors depending on system options.

The bad flavor uses a static buffer for returning results, so if
you save the pointer from strerror_r, the string you're referencing
becomes useless if anybody else calls strerror_r again

The good flavor does what you expect: it writes the error to your buffer.

This commit uses strerror_r directly if it's a good version or copies
the static buffer into our private buffer if it's a bad version.

Thanks to gemorin for explaining the problem and drafting a fix.

Fixes #239
2015-01-05 16:53:23 -05:00
tzickel
ba3e74c408 Refactor reading code into read.c
Makes hiredis reading functions easier to include in external projects

[fixed all merge conflicts against current version]

Closes #249
2015-01-05 16:53:22 -05:00
Matt Stancliff
6a00a4643b Fix redisAppendCommand error result
Previously, redisvAppendCommand() would return OOM even with formatting
errors.  Now we use OTHER with an error string telling the user the
error was formatting related, not memory related.

This also fixes a potentially worse bug where were would pass error result
of -1 as an actual length to another function taking an unsigned length,
which would result in crash/overallocation/errors.  Now for that case,
we just return an error immediately and stop processing the command.

Fixes #177
2015-01-05 16:52:08 -05:00
NanXiao
e30c96ebde Add empty pointer check in error cleanup
If realloc fails, the curargv will be NULL.

Closes #253
2015-01-05 16:39:30 -05:00
NanXiao
d1e820d8ee Add error check in redisContextInit
Check whether the obuf or reader is empty: if one of the items is empty,
free the redisContext.
2015-01-05 16:39:30 -05:00
Nan Xiao
b6a860795c Fix redisvFormatCommand format parsing
Flags can occur in any order in format string, so we can't assume any order.

In original code, the redisvFormatCommand can process " %#+d" correctly,
but can't process "%+#d".

Closes #257
2015-01-05 16:39:30 -05:00
Matt Stancliff
9a753b4251 Add API to free hiredis (sds) formattings
External callers may not know about sdsfree, so let's
give them an easy way to know how to free their sds result.
2015-01-05 16:39:30 -05:00