Skip to content

Commit 143b20f

Browse files
committed
feat(cmd): memory allocation errors to SET command
1 parent 0e67a35 commit 143b20f

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

docs/COMMANDS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,11 +823,12 @@ RENAME name user_name
823823
**Since**: `0.1.0`
824824
**Time complexity**: `O(1)`
825825
**Permissions**: (`P_READ` if used `GET` argument) and `P_WRITE`
826-
**Returns**: `OK` or a value or `ERROR`
826+
**Returns**: `OK` or a value or an error message
827827
**Behavior**:
828828
* If the key is exist, new value will be overwritten.
829829
* If value type is not specified using `AS type` argument, type will be determined by value itself. For example, `boolean` type for `true` value.
830830
* If value type is specified using `AS type` and value does not match the type, throws an error.
831+
* If a memory allocation is failed, throws an error.
831832

832833
**Arguments**:
833834
- **NX**: Only set if the key does not exist.

src/commands/kv/set.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,11 @@ static string_t run(struct CommandEntry *entry) {
164164
break;
165165

166166
case TELLY_STR:
167-
take_as_string(&value, entry->args->data[1]);
167+
if (take_as_string(&value, entry->args->data[1]) == -1) {
168+
PASS_NO_CLIENT(entry->client);
169+
return RESP_ERROR_MESSAGE("Out of memory");
170+
}
171+
168172
break;
169173

170174
default:
@@ -184,7 +188,11 @@ static string_t run(struct CommandEntry *entry) {
184188
break;
185189

186190
case TELLY_STR:
187-
take_as_string(&value, entry->args->data[1]);
191+
if (take_as_string(&value, entry->args->data[1]) == -1) {
192+
PASS_NO_CLIENT(entry->client);
193+
return RESP_ERROR_MESSAGE("Out of memory");
194+
}
195+
188196
break;
189197

190198
default:
@@ -198,12 +206,20 @@ static string_t run(struct CommandEntry *entry) {
198206
return RESP_ERROR_MESSAGE("The type must be string for this value");
199207
}
200208

201-
take_as_string(&value, entry->args->data[1]);
209+
if (take_as_string(&value, entry->args->data[1]) == -1) {
210+
PASS_NO_CLIENT(entry->client);
211+
return RESP_ERROR_MESSAGE("Out of memory");
212+
}
202213
}
203214

204215
if (get) {
205216
if (entry->password->permissions & P_READ) {
206-
set_data(entry->database, res, key, value, type);
217+
const bool success = (set_data(entry->database, res, key, value, type) != NULL);
218+
219+
if (!success) {
220+
PASS_NO_CLIENT(entry->client);
221+
return RESP_ERROR_MESSAGE("Out of memory");
222+
}
207223

208224
if (res) {
209225
if (entry->client) {
@@ -224,7 +240,7 @@ static string_t run(struct CommandEntry *entry) {
224240
if (success) {
225241
return RESP_OK();
226242
} else {
227-
return RESP_ERROR();
243+
return RESP_ERROR_MESSAGE("Out of memory");
228244
}
229245
}
230246
}

0 commit comments

Comments
 (0)