Assert that eMessageText MUST be serialized last.
[citadel.git] / citadel / server / msgbase.c
index 40d1c8e5b6083e0321b435f0a56983bf40d763ac..259a3e5ab2994febe129e78febbdca89f6b31015 100644 (file)
@@ -2295,7 +2295,6 @@ int CtdlSaveMsgPointerInRoom(char *roomname, long msgid, int do_repl_check, stru
 // called by server-side modules.
 long CtdlSaveThisMessage(struct CtdlMessage *msg, long msgid, int Reply) {
        long retval;
-       struct ser_ret smr;
        int is_bigmsg = 0;
        char *holdM = NULL;
        long holdMLen = 0;
@@ -2310,7 +2309,7 @@ long CtdlSaveThisMessage(struct CtdlMessage *msg, long msgid, int Reply) {
        }
 
        // Serialize our data structure for storage in the database
-       CtdlSerializeMessage(&smr, msg);
+       struct ser_ret smr = CtdlSerializeMessage(msg);
 
        if (is_bigmsg) {
                // put the message body back into the message
@@ -2380,58 +2379,54 @@ long send_message(struct CtdlMessage *msg) {
 
 // Serialize a struct CtdlMessage into the format used on disk.
 // 
-// This function loads up a "struct ser_ret" (defined in server.h) which
+// This function returns a "struct ser_ret" (defined in server.h) which
 // contains the length of the serialized message and a pointer to the
 // serialized message in memory.  THE LATTER MUST BE FREED BY THE CALLER.
-void CtdlSerializeMessage(struct ser_ret *ret,         // return values
-                         struct CtdlMessage *msg)      // unserialized msg
-{
+struct ser_ret CtdlSerializeMessage(struct CtdlMessage *msg) {
+       struct ser_ret ret;
        size_t wlen;
        int i;
 
        // Check for valid message format
        if (CM_IsValidMsg(msg) == 0) {
                syslog(LOG_ERR, "msgbase: CtdlSerializeMessage() aborting due to invalid message");
-               ret->len = 0;
-               ret->ser = NULL;
-               return;
+               ret.len = 0;
+               ret.ser = NULL;
+               return(ret);
        }
 
-       ret->len = 3;
-       for (i=0; i < NDiskFields; ++i)
-               if (msg->cm_fields[FieldOrder[i]] != NULL)
-                       ret->len += msg->cm_lengths[FieldOrder[i]] + 2;
+       ret.len = 3;
+       assert(FieldOrder[NDiskFields-1] == eMessageText);              // Message text MUST be last!
+       for (i=0; i < NDiskFields; ++i) {
+               if (msg->cm_fields[FieldOrder[i]] != NULL) {
+                       ret.len += msg->cm_lengths[FieldOrder[i]] + 2;
+               }
+       }
 
-       ret->ser = malloc(ret->len);
-       if (ret->ser == NULL) {
-               syslog(LOG_ERR, "msgbase: CtdlSerializeMessage() malloc(%ld) failed: %m", (long)ret->len);
-               ret->len = 0;
-               ret->ser = NULL;
-               return;
+       ret.ser = malloc(ret.len);
+       if (ret.ser == NULL) {
+               syslog(LOG_ERR, "msgbase: CtdlSerializeMessage() malloc(%ld) failed: %m", (long)ret.len);
+               ret.len = 0;
+               ret.ser = NULL;
+               return(ret);
        }
 
-       ret->ser[0] = 0xFF;
-       ret->ser[1] = msg->cm_anon_type;
-       ret->ser[2] = msg->cm_format_type;
+       ret.ser[0] = 0xFF;
+       ret.ser[1] = msg->cm_anon_type;
+       ret.ser[2] = msg->cm_format_type;
        wlen = 3;
 
        for (i=0; i < NDiskFields; ++i) {
                if (msg->cm_fields[FieldOrder[i]] != NULL) {
-                       ret->ser[wlen++] = (char)FieldOrder[i];
-
-                       memcpy(&ret->ser[wlen],
-                              msg->cm_fields[FieldOrder[i]],
-                              msg->cm_lengths[FieldOrder[i]] + 1);
-
+                                                                       // future improvement: do the bigmsg check right here
+                       ret.ser[wlen++] = (char)FieldOrder[i];
+                       memcpy(&ret.ser[wlen], msg->cm_fields[FieldOrder[i]], msg->cm_lengths[FieldOrder[i]] + 1);
                        wlen = wlen + msg->cm_lengths[FieldOrder[i]] + 1;
                }
        }
 
-       if (ret->len != wlen) {
-               syslog(LOG_ERR, "msgbase: ERROR; len=%ld wlen=%ld", (long)ret->len, (long)wlen);
-       }
-
-       return;
+       assert(ret.len == wlen);                                        // Make sure we measured it correctly
+       return(ret);
 }
 
 
@@ -2530,13 +2525,16 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,     // message to save
                        string_trim(content_type);
                        aptr = content_type;
                        while (!IsEmptyStr(aptr)) {
-                               if ((*aptr == ';')
-                                   || (*aptr == ' ')
-                                   || (*aptr == 13)
-                                   || (*aptr == 10)) {
+                               if (    (*aptr == ';')
+                                       || (*aptr == ' ')
+                                       || (*aptr == 13)
+                                       || (*aptr == 10)
+                               ) {
                                        *aptr = 0;
                                }
-                               else aptr++;
+                               else {
+                                       aptr++;
+                               }
                        }
                }
        }
@@ -3024,10 +3022,12 @@ struct CtdlMessage *CtdlMakeMessageLen(
                        long IsAscii;
                        IsAscii = -1;
                        i = 0;
-                       while ((subject[i] != '\0') && (IsAscii = isascii(subject[i]) != 0 ))
+                       while ((subject[i] != '\0') && (IsAscii = isascii(subject[i]) != 0 )) {
                                i++;
-                       if (IsAscii != 0)
+                       }
+                       if (IsAscii != 0) {
                                CM_SetField(msg, eMsgSubject, subject);
+                       }
                        else {  // ok, we've got utf8 in the string.
                                char *rfc2047Subj;
                                rfc2047Subj = rfc2047encode(subject, length);
@@ -3207,11 +3207,7 @@ void PutMetaData(struct MetaData *smibuf) {
 
        // Use the negative of the message number for the metadata db index
        TheIndex = (0L - smibuf->meta_msgnum);
-
-       cdb_store(CDB_MSGMAIN,
-                 &TheIndex, (int)sizeof(long),
-                 smibuf, (int)sizeof(struct MetaData)
-       );
+       cdb_store(CDB_MSGMAIN, &TheIndex, (int)sizeof(long), smibuf, (int)sizeof(struct MetaData));
 }