Skip to content

Commit 1e2ccb6

Browse files
committed
Make JSON encoding more efficient and robust
1 parent 39d37e5 commit 1e2ccb6

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

data.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@
305305
if (!is_numeric($data['idx'])) fatalerr('Invalid webauthn request');
306306
$keys = json_decode(query_setting('webauthnkeys', '[]', true));
307307
array_splice($keys, $data['idx'], 1);
308-
store_setting('webauthnkeys', json_encode($keys));
308+
store_setting('webauthnkeys', json_encode($keys, JSON_THROW_ON_ERROR));
309309
foreach ($keys as $key) {
310310
$ret[] = $key['name'] ?? dechex(crc32(implode('', $key->id)));
311311
}
@@ -805,7 +805,7 @@ function($row) use ($note) {
805805

806806
if (!($stmt->execute([ $note['content'], $note['title'], $pinned, $cursor, $note['mode'] ?? 'edit', $id ]))) {
807807
$processUser = posix_getpwuid(posix_geteuid());
808-
error_log("update_note() update execute failed: " . $stmt->errorInfo()[2] . ' (userid: ' . json_encode($processUser) . ')');
808+
error_log("update_note() update execute failed: " . $stmt->errorInfo()[2] . ' (userid: ' . json_encode($processUser, JSON_THROW_ON_ERROR) . ')');
809809
return [];
810810
}
811811

@@ -907,7 +907,7 @@ function update_note_meta($id, $note) {
907907
}
908908
if (!($stmt->execute([ $pinned, $cursor, $note['mode'] ?? 'edit', $id ]))) {
909909
$processUser = posix_getpwuid(posix_geteuid());
910-
error_log("FlowNotes: update_note_meta() update execute failed: " . $stmt->errorInfo()[2] . ' (userid: ' . json_encode($processUser) . ')');
910+
error_log("FlowNotes: update_note_meta() update execute failed: " . $stmt->errorInfo()[2] . ' (userid: ' . json_encode($processUser, JSON_THROW_ON_ERROR) . ')');
911911
return 0;
912912
}
913913
return $pinned;
@@ -1156,12 +1156,13 @@ function sql_updatecount($query, $params = []) {
11561156

11571157
function send_and_exit($data) {
11581158
header('Content-type: application/json');
1159-
print json_encode($data);
1159+
print json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_INVALID_UTF8_SUBSTITUTE);
1160+
if (json_last_error()) error_log('FlowNotes JSON encode error: ' . json_last_error_msg());
11601161
exit();
11611162
}
11621163
function fatalerr($msg) {
11631164
header('Content-type: application/json');
1164-
print json_encode([ 'error' => $msg ]);
1165+
print json_encode([ 'error' => $msg ], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_INVALID_UTF8_SUBSTITUTE|JSON_THROW_ON_ERROR);
11651166
exit(1);
11661167
}
11671168
function dbg($var) {

0 commit comments

Comments
 (0)