Fixing SOGO error NSDataMalloc(instance) does not recognize stringByTrimmingSpaces

My SOGo (calDAV) server stopped syncing recently.

Digging into the logs, I saw an error, which was also replicated when running sogo-tool:

<0x0x556f05e09480[SOGoCache]> Using host(s) '127.0.0.1' as server(s)
2023-12-05 14:53:58.230 sogo-tool[20884:20884] Checking folders of user my_account
2023-12-05 14:53:58.237 sogo-tool[20884:20884] MySQL4 connection established 0x0x556f05e0d060
2023-12-05 14:53:58.238 sogo-tool[20884:20884] ---------- -[MySQL4Channel openChannel]: <MySQL4Channel[0x0x556f05e9c430] connection=0x0x556f05e0d060> opens channel count[0]
2023-12-05 14:53:58.238 sogo-tool[20884:20884] MySQL4 channel 0x0x556f05e9c430 opened (connection=0x0x556f05e0d060,sogo)
2023-12-05 14:53:58.238 sogo-tool[20884:20884] <MySQL4Channel[0x0x556f05e9c430] connection=0x0x556f05e0d060> SQL: SELECT c_path FROM sogo_folder_info WHERE c_path1 = 'Users' AND c_path2 = 'my_account';
2023-12-05 14:53:58.239 sogo-tool[20884:20884] <MySQL4Channel[0x0x556f05e9c430] connection=0x0x556f05e0d060>   query has results, entering fetch-mode.
2023-12-05 14:53:58.239 sogo-tool[20884:20884] <MySQL4Channel[0x0x556f05e9c430] connection=0x0x556f05e0d060> SQL: SELECT c_folder_id, c_path, c_location, c_quick_location, c_acl_location, c_folder_type FROM sogo_folder_info WHERE c_path1 = 'Users' AND c_path2 = 'my_account' AND c_path3 = 'Calendar' AND c_path4 = 'personal';
2023-12-05 14:53:58.240 sogo-tool[20884:20884] <MySQL4Channel[0x0x556f05e9c430] connection=0x0x556f05e0d060>   query has results, entering fetch-mode.
2023-12-05 14:53:58.240 sogo-tool[20884:20884] <MySQL4Channel[0x0x556f05e9c430] connection=0x0x556f05e0d060> SQL: SELECT c_name, c_content FROM sogo_store WHERE c_folder_id = 4 AND (c_deleted != 1 OR c_deleted IS NULL);
2023-12-05 14:53:58.245 sogo-tool[20884:20884] <MySQL4Channel[0x0x556f05e9c430] connection=0x0x556f05e0d060>   query has results, entering fetch-mode.
sogo-tool: Uncaught exception NSInvalidArgumentException, reason: NSDataMalloc(instance) does not recognize stringByTrimmingSpaces

Fixing the database collation

The problem was the collation of c_content in sogo_store, in the MySQL database:

sogo_store | CREATE TABLE `sogo_store` (
  `c_folder_id` int(11) NOT NULL,
  `c_name` varchar(255) NOT NULL,
  `c_content` mediumtext DEFAULT NULL,
  `c_creationdate` int(11) NOT NULL,
  `c_lastmodified` int(11) NOT NULL,
  `c_version` int(11) NOT NULL,
  `c_deleted` int(11) DEFAULT NULL,
  PRIMARY KEY (`c_folder_id`,`c_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin ROW_FORMAT=DYNAMIC |

Apparently, utf8mb4_bin was not the collation I wanted (even though it is referenced in this bug solution (which has now been corrected on the SOGO bug system)).

I fixed it with:

ALTER TABLE sogo_store MODIFY c_content MEDIUMTEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

(having logged in, and using the sogo database).

I also unsubscribed and resubscribed my calendars, and it is working again.