OSSP CVS Repository

ossp - Difference in ossp-pkg/rc/rc_sect.c versions 1.19 and 1.20
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [History

ossp-pkg/rc/rc_sect.c 1.19 -> 1.20

--- rc_sect.c    2003/07/07 13:30:51     1.19
+++ rc_sect.c    2003/07/08 15:09:50     1.20
@@ -46,15 +46,13 @@
     /* Among other things, they make great coffee at Cable & Wireless */
     /* This code would probably have more bugs if the coffee was not as good */
     pSec = (rc_section_t *)calloc(1, sizeof(rc_section_t));
-
-    if (pSec) {
-        pSec->m_szName = malloc((strlen(szName) + 1) * sizeof(char));
-        strcpy(pSec->m_szName, szName);
-        pSec->m_pData = scriptNew();
-    }
-    else
+    if (pSec == NULL)
         RC_THROW(RC_ERR_MEM);
 
+    pSec->m_szName = malloc((strlen(szName) + 1) * sizeof(char));
+    strcpy(pSec->m_szName, szName);
+    pSec->m_pData = scriptNew();
+
     return(pSec);
 }
 
@@ -73,21 +71,21 @@
 
     /* Deep copy of section name */
     if (pOrigsec->m_szName) {
-        pSec->m_szName = malloc((strlen(pOrigsec->m_szName) + sizeof(char))\
+        pSec->m_szName = malloc((strlen(pOrigsec->m_szName) + 1)\
             * sizeof(char));
         strcpy(pSec->m_szName, pOrigsec->m_szName);
     }
 
     /* Deep copy of parent name */
     if (pOrigsec->m_szParent) {
-        pSec->m_szParent = malloc((strlen(pOrigsec->m_szParent) + sizeof(char))\
+        pSec->m_szParent = malloc((strlen(pOrigsec->m_szParent) + 1)\
             * sizeof(char));
         strcpy(pSec->m_szParent, pOrigsec->m_szParent);
     }
 
     /* Deep copy of user name */
     if (pOrigsec->m_szLogin) {
-        pSec->m_szLogin = malloc((strlen(pOrigsec->m_szLogin) + sizeof(char))\
+        pSec->m_szLogin = malloc((strlen(pOrigsec->m_szLogin) + 1)\
             * sizeof(char));
         strcpy(pSec->m_szLogin, pOrigsec->m_szLogin);
     }
@@ -171,8 +169,10 @@
 
 const char *sectionGetdata(rc_section_t *pSec)
 { /* Data of section, this is the script body of the particular section */
+  /* ATTENTION: data section may be NULL */
     if (pSec) {
         const char *kszScriptdata = scriptGetdata(pSec->m_pData);
+        /* FIXME mlelstv -- why is an empty section NULL ? */
         if (kszScriptdata && strlen(kszScriptdata) > 0)
             return(kszScriptdata);
         else
@@ -248,7 +248,7 @@
 
     if (scriptGetdata(pSec->m_pData)) { /* The section data is already in use */
         scriptDelete(pSec->m_pData);
-        pSec->m_pData = NULL;
+        pSec->m_pData = scriptNew();
     }
 
     scriptSetdata(pSec->m_pData, kszIn);
@@ -261,17 +261,14 @@
     char *szTemp  = NULL;
     size_t nBytes = (Len + 1) * sizeof(char);       /* Set size */
 
-    assert(pSec && kszIn); /* Dummy detector */
-
-    if (pSec->m_pData) {    /* The section data is already in use */
-        scriptDelete(pSec->m_pData);
-    }
-
-    pSec->m_pData = scriptNew();
+    /* copy data with terminating NUL character */
     szTemp = malloc(nBytes);
     strncpy(szTemp, kszIn, Len);
     *(szTemp + Len) = '\0';                 /* Terminate outgoing */
-    scriptSetdata(pSec->m_pData, szTemp);   /* Finish the job */
+
+    /* FIXME mlelstv -- how to do exception handling ?? */
+    sectionSetdata(pSec, szTemp);           /* Finish the job */
+
     free(szTemp);                           /* Deallocate */
     szTemp = NULL;
     return(RC_THROW(RC_OK));
@@ -301,23 +298,32 @@
 ************************************************/
 rc_return_t sectionWrite(rc_section_t *pSec, const char *szPath)
 {
-    int nFdtmp = open(szPath, O_WRONLY | O_CREAT, 0600);
+    int nFdtmp = -1;
     FILE *pStream = NULL;
 
-    /* Initial sanity checks */
-    if (!pSec || nFdtmp < 0)
+    /* Parameter sanity checks */
+    if (!pSec)
         return(RC_THROW(RC_ERR_USE));
-    else
-        pStream = fdopen(nFdtmp, "w");
 
-    if (pStream) {
-        fprintf(pStream, "#su %s\n", sectionGetlogin(pSec));
-        fprintf(pStream, "%s", sectionGetdata(pSec));
-        fclose(pStream);
-        return(RC_THROW(RC_OK));
-    }
-    else
+    /* open file with restricted mode 0600 to preserve privacy */
+    nFdtmp = open(szPath, O_WRONLY | O_CREAT, 0600);
+    if (nFdtmp < 0)
+        return(RC_THROW(RC_ERR_USE));
+
+    pStream = fdopen(nFdtmp, "w");
+    if (pStream == NULL) {
+        close(nFdtmp);
+        unlink(szPath);
         return(RC_THROW(RC_ERR_USE));
+    }
+
+    fprintf(pStream, "#su %s\n", sectionGetlogin(pSec));
+    fprintf(pStream, "%s", sectionGetdata(pSec));
+    fclose(pStream);
+
+    /* this file is deleted by user, no cleanup necessary */
+
+    return(RC_THROW(RC_OK));
 }
 
 /************************************************
@@ -343,7 +349,7 @@
         free(pSec);
     }
     else    /* Dumbass passed an empty section object */
-        assert(FALSE);
+        return(RC_THROW(RC_ERR_USE));
 
     return(RC_THROW(RC_OK));
 }

CVSTrac 2.0.1