// Retrieve a list of all messages (message numbers) in the specified room.
// Returns the number of messages in the room, allocates a pointer to the array and stuffs it in the supplied location.
// Caller must free that memory.
-// Returns (-1) if error.
+// If no messages in room, returns 0 and msgs is set to NULL.
int CtdlFetchMsgList(long roomnum, long **msgs) {
int num_msgs = 0;
struct cdbdata *cdbfr;
if (cdbfr == NULL) {
syslog(LOG_ERR, "room_ops: no msglist for room %ld", roomnum);
*msgs = NULL;
- return (-1);
+ return (0);
}
- *msgs = malloc(cdbfr->len);
- memcpy(*msgs, cdbfr->ptr, cdbfr->len);
num_msgs = cdbfr->len / sizeof(long);
+ if (num_msgs > 0) {
+ *msgs = malloc(cdbfr->len);
+ memcpy(*msgs, cdbfr->ptr, cdbfr->len);
+ }
+ else {
+ *msgs = NULL;
+ }
cdb_free(cdbfr);
// BEGIN diagnostic section remove this
syslog(LOG_DEBUG, "\033[7mCtdlFetchMsgList(%ld) %d messages\033[0m", roomnum, num_msgs);