Protect precious strlens, as pointed out by John Goerzen
[citadel.git] / citadel / msgbase.c
index d8b1ac9d2bd6b573806c92294731f0b81f4c1972..49f634595e8f4259a18034e36247632aa6a7a064 100644 (file)
@@ -1766,7 +1766,7 @@ void OutputCtdlMsgHeaders(
 void OutputRFC822MsgHeaders(
        struct CtdlMessage *TheMessage,
        int flags,              /* should the bessage be exported clean */
-       const char *nl,
+       const char *nl, int nlen,
        char *mid, long sizeof_mid,
        char *suser, long sizeof_suser,
        char *luser, long sizeof_luser,
@@ -1904,7 +1904,7 @@ void Dump_RFC822HeadersBody(
        int headers_only,       /* eschew the message body? */
        int flags,              /* should the bessage be exported clean? */
 
-       const char *nl)
+       const char *nl, int nlen)
 {
        cit_uint8_t prev_ch;
        int eoh = 0;
@@ -1913,6 +1913,7 @@ void Dump_RFC822HeadersBody(
        int outlen = 0;
        int nllen = strlen(nl);
        char *mptr;
+       int lfSent = 0;
 
        mptr = TheMessage->cm_fields[eMesageText];
 
@@ -1969,12 +1970,16 @@ void Dump_RFC822HeadersBody(
                                MSGM_syslog(LOG_ERR, "Dump_RFC822HeadersBody(): aborting due to write failure.\n");
                                return;
                        }
+                       lfSent =  (outbuf[outlen - 1] == '\n');
                        outlen = 0;
                }
        }
        if (outlen > 0) {
                client_write(outbuf, outlen);
+               lfSent =  (outbuf[outlen - 1] == '\n');
        }
+       if (!lfSent)
+               client_write(nl, nlen);
 }
 
 
@@ -1986,13 +1991,12 @@ void Dump_RFC822HeadersBody(
 void DumpFormatFixed(
        struct CtdlMessage *TheMessage,
        int mode,               /* how would you like that message? */
-       const char *nl)
+       const char *nl, int nllen)
 {
        cit_uint8_t ch;
        char buf[SIZ];
        int buflen;
        int xlline = 0;
-       int nllen = strlen (nl);
        char *mptr;
 
        mptr = TheMessage->cm_fields[eMesageText];
@@ -2067,6 +2071,7 @@ int CtdlOutputPreLoadedMsg(
        struct CitContext *CCC = CC;
        int i;
        const char *nl; /* newline string */
+       int nlen;
        struct ma_info ma;
 
        /* Buffers needed for RFC822 translation.  These are all filled
@@ -2085,6 +2090,7 @@ int CtdlOutputPreLoadedMsg(
 
        strcpy(mid, "unknown");
        nl = (crlf ? "\r\n" : "\n");
+       nlen = crlf ? 2 : 1;
 
        if (!CM_IsValidMsg(TheMessage)) {
                MSGM_syslog(LOG_ERR,
@@ -2185,7 +2191,7 @@ int CtdlOutputPreLoadedMsg(
                OutputRFC822MsgHeaders(
                        TheMessage,
                        flags,
-                       nl,
+                       nl, nlen,
                        mid, sizeof(mid),
                        suser, sizeof(suser),
                        luser, sizeof(luser),
@@ -2249,7 +2255,7 @@ START_TEXT:
                                TheMessage,
                                headers_only,
                                flags,
-                               nl);
+                               nl, nlen);
                        goto DONE;
                }
        }
@@ -2267,7 +2273,7 @@ START_TEXT:
                DumpFormatFixed(
                        TheMessage,
                        mode,           /* how would you like that message? */
-                       nl);
+                       nl, nlen);
 
        /* If the message on disk is format 0 (Citadel vari-format), we
         * output using the formatter at 80 columns.  This is the final output
@@ -2826,7 +2832,7 @@ long CtdlSubmitMsg(struct CtdlMessage *msg,       /* message to save */
        /*
         * If this message has no O (room) field, generate one.
         */
-       if (CM_IsEmpty(msg, eOriginalRoom)) {
+       if (CM_IsEmpty(msg, eOriginalRoom) && !IsEmptyStr(CCC->room.QRname)) {
                CM_SetField(msg, eOriginalRoom, CCC->room.QRname, strlen(CCC->room.QRname));
        }
 
@@ -3049,10 +3055,10 @@ void quickie_message(const char *from,
        msg->cm_anon_type = MES_NORMAL;
        msg->cm_format_type = format_type;
 
-       if (from != NULL) {
+       if (!IsEmptyStr(from)) {
                CM_SetField(msg, eAuthor, from, strlen(from));
        }
-       else if (fromaddr != NULL) {
+       else if (!IsEmptyStr(fromaddr)) {
                char *pAt;
                CM_SetField(msg, eAuthor, fromaddr, strlen(fromaddr));
                pAt = strchr(msg->cm_fields[eAuthor], '@');
@@ -3064,17 +3070,19 @@ void quickie_message(const char *from,
                msg->cm_fields[eAuthor] = strdup("Citadel");
        }
 
-       if (fromaddr != NULL) CM_SetField(msg, erFc822Addr, fromaddr, strlen(fromaddr));
-       if (room != NULL) CM_SetField(msg, eOriginalRoom, room, strlen(room));
+       if (!IsEmptyStr(fromaddr)) CM_SetField(msg, erFc822Addr, fromaddr, strlen(fromaddr));
+       if (!IsEmptyStr(room)) CM_SetField(msg, eOriginalRoom, room, strlen(room));
        CM_SetField(msg, eNodeName, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")));
-       if (to != NULL) {
+       if (!IsEmptyStr(to)) {
                CM_SetField(msg, eRecipient, to, strlen(to));
                recp = validate_recipients(to, NULL, 0);
        }
-       if (subject != NULL) {
+       if (!IsEmptyStr(subject)) {
                CM_SetField(msg, eMsgSubject, subject, strlen(subject));
        }
-       CM_SetField(msg, eMesageText, text, strlen(text));
+       if (!IsEmptyStr(text)) {
+               CM_SetField(msg, eMesageText, text, strlen(text));
+       }
 
        CtdlSubmitMsg(msg, recp, room, 0);
        CM_Free(msg);
@@ -3514,7 +3522,7 @@ struct CtdlMessage *CtdlMakeMessageLen(
        if (myelen > 0) {
                CM_SetField(msg, eMessagePath, my_email, myelen);
        }
-       else {
+       else if (!IsEmptyStr(author->fullname)) {
                CM_SetField(msg, eMessagePath, author->fullname, strlen(author->fullname));
        }
        convert_spaces_to_underscores(msg->cm_fields[eMessagePath]);
@@ -3532,11 +3540,13 @@ struct CtdlMessage *CtdlMakeMessageLen(
        CM_SetAsFieldSB(msg, eAuthor, &FakeEncAuthor);
        FreeStrBuf(&FakeAuthor);
 
-       if (CCC->room.QRflags & QR_MAILBOX) {           /* room */
-               CM_SetField(msg, eOriginalRoom, &CCC->room.QRname[11], strlen(&CCC->room.QRname[11]));
-       }
-       else {
-               CM_SetField(msg, eOriginalRoom, CCC->room.QRname, strlen(CCC->room.QRname));
+       if (!!IsEmptyStr(CCC->room.QRname)) {
+               if (CCC->room.QRflags & QR_MAILBOX) {           /* room */
+                       CM_SetField(msg, eOriginalRoom, &CCC->room.QRname[11], strlen(&CCC->room.QRname[11]));
+               }
+               else {
+                       CM_SetField(msg, eOriginalRoom, CCC->room.QRname, strlen(CCC->room.QRname));
+               }
        }
 
        CM_SetField(msg, eNodeName, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")));