This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
use lengths in sv_setpv() calls
[perl5.git] / util.c
diff --git a/util.c b/util.c
index 5c1cdea..dfd912f 100644 (file)
--- a/util.c
+++ b/util.c
 #endif
 #endif
 
+#ifdef __Lynx__
+/* Missing protos on LynxOS */
+int putenv(char *);
+#endif
+
 #ifdef I_SYS_WAIT
 #  include <sys/wait.h>
 #endif
@@ -616,7 +621,7 @@ Perl_fbm_instr(pTHX_ unsigned char *big, register unsigned char *bigend, SV *lit
 
 /* start_shift, end_shift are positive quantities which give offsets
    of ends of some substring of bigstr.
-   If `last' we want the last occurrence.
+   If "last" we want the last occurrence.
    old_posp is the way of communication between consequent calls if
    the next call needs to find the .
    The initial *old_posp should be -1.
@@ -870,6 +875,7 @@ S_mess_alloc(pTHX)
     Newz(905, any, 1, XPVMG);
     SvFLAGS(sv) = SVt_PVMG;
     SvANY(sv) = (void*)any;
+    SvPV_set(sv, 0);
     SvREFCNT(sv) = 1 << 30; /* practically infinite */
     PL_mess_sv = sv;
     return sv;
@@ -1008,7 +1014,7 @@ Perl_vmess(pTHX_ const char *pat, va_list *args)
            OutCopFILE(cop), (IV)CopLINE(cop));
        if (GvIO(PL_last_in_gv) && IoLINES(GvIOp(PL_last_in_gv))) {
            const bool line_mode = (RsSIMPLE(PL_rs) &&
-                             SvCUR(PL_rs) == 1 && *SvPVX(PL_rs) == '\n');
+                             SvCUR(PL_rs) == 1 && *SvPVX_const(PL_rs) == '\n');
            Perl_sv_catpvf(aTHX_ sv, ", <%s> %s %"IVdf,
                           PL_last_in_gv == PL_argvgv ?
                           "" : GvNAME(PL_last_in_gv),
@@ -1361,10 +1367,10 @@ Perl_vwarner(pTHX_ U32  err, const char* pat, va_list* args)
 {
     dVAR;
     if (ckDEAD(err)) {
-       SV *msv = vmess(pat, args);
+       SV * const msv = vmess(pat, args);
        STRLEN msglen;
        const char *message = SvPV(msv, msglen);
-       I32 utf8 = SvUTF8(msv);
+       const I32 utf8 = SvUTF8(msv);
 
        if (PL_diehook) {
            assert(message);
@@ -2710,7 +2716,7 @@ Perl_repeatcpy(pTHX_ register char *to, register const char *from, I32 len, regi
 
 #ifndef HAS_RENAME
 I32
-Perl_same_dirent(pTHX_ char *a, char *b)
+Perl_same_dirent(pTHX_ const char *a, const char *b)
 {
     char *fa = strrchr(a,'/');
     char *fb = strrchr(b,'/');
@@ -2729,16 +2735,16 @@ Perl_same_dirent(pTHX_ char *a, char *b)
     if (strNE(a,b))
        return FALSE;
     if (fa == a)
-       sv_setpv(tmpsv, ".");
+       sv_setpvn(tmpsv, ".", 1);
     else
        sv_setpvn(tmpsv, a, fa - a);
-    if (PerlLIO_stat(SvPVX(tmpsv), &tmpstatbuf1) < 0)
+    if (PerlLIO_stat(SvPVX_const(tmpsv), &tmpstatbuf1) < 0)
        return FALSE;
     if (fb == b)
-       sv_setpv(tmpsv, ".");
+       sv_setpvn(tmpsv, ".", 1);
     else
        sv_setpvn(tmpsv, b, fb - b);
-    if (PerlLIO_stat(SvPVX(tmpsv), &tmpstatbuf2) < 0)
+    if (PerlLIO_stat(SvPVX_const(tmpsv), &tmpstatbuf2) < 0)
        return FALSE;
     return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
           tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
@@ -3354,8 +3360,11 @@ Perl_init_tm(pTHX_ struct tm *ptm)       /* see mktime, strftime and asctime */
 {
 #ifdef HAS_TM_TM_ZONE
     Time_t now;
+    struct tm* my_tm;
     (void)time(&now);
-    Copy(localtime(&now), ptm, 1, struct tm);
+    my_tm = localtime(&now);
+    if (my_tm)
+        Copy(my_tm, ptm, 1, struct tm);
 #endif
 }
 
@@ -3679,8 +3688,7 @@ Perl_getcwd_sv(pTHX_ register SV *sv)
         * size from the heap if they are given a NULL buffer pointer.
         * The problem is that this behaviour is not portable. */
        if (getcwd(buf, sizeof(buf) - 1)) {
-           STRLEN len = strlen(buf);
-           sv_setpvn(sv, buf, len);
+           sv_setpvn(sv, buf, strlen(buf));
            return TRUE;
        }
        else {
@@ -3693,8 +3701,7 @@ Perl_getcwd_sv(pTHX_ register SV *sv)
 
     Stat_t statbuf;
     int orig_cdev, orig_cino, cdev, cino, odev, oino, tdev, tino;
-    int namelen, pathlen=0;
-    DIR *dir;
+    int pathlen=0;
     Direntry_t *dp;
 
     (void)SvUPGRADE(sv, SVt_PV);
@@ -3709,6 +3716,7 @@ Perl_getcwd_sv(pTHX_ register SV *sv)
     cino = orig_cino;
 
     for (;;) {
+       DIR *dir;
        odev = cdev;
        oino = cino;
 
@@ -3731,9 +3739,9 @@ Perl_getcwd_sv(pTHX_ register SV *sv)
 
        while ((dp = PerlDir_read(dir)) != NULL) {
 #ifdef DIRNAMLEN
-           namelen = dp->d_namlen;
+           const int namelen = dp->d_namlen;
 #else
-           namelen = strlen(dp->d_name);
+           const int namelen = strlen(dp->d_name);
 #endif
            /* skip . and .. */
            if (SV_CWD_ISDOT(dp)) {
@@ -3763,7 +3771,7 @@ Perl_getcwd_sv(pTHX_ register SV *sv)
 
        if (pathlen) {
            /* shift down */
-           Move(SvPVX(sv), SvPVX(sv) + namelen + 1, pathlen, char);
+           Move(SvPVX_const(sv), SvPVX(sv) + namelen + 1, pathlen, char);
        }
 
        /* prepend current directory to the front */
@@ -3785,7 +3793,7 @@ Perl_getcwd_sv(pTHX_ register SV *sv)
        *SvEND(sv) = '\0';
        SvPOK_only(sv);
 
-       if (PerlDir_chdir(SvPVX(sv)) < 0) {
+       if (PerlDir_chdir(SvPVX_const(sv)) < 0) {
            SV_CWD_RETURN_UNDEF;
        }
     }
@@ -3967,7 +3975,7 @@ Perl_new_version(pTHX_ SV *ver)
        AvREAL_on((AV*)sv);
        for ( key = 0; key <= av_len(av); key++ )
        {
-           I32 rev = SvIV(*av_fetch(av, key, FALSE));
+           const I32 rev = SvIV(*av_fetch(av, key, FALSE));
            av_push((AV *)sv, newSViv(rev));
        }
        return rv;
@@ -4351,7 +4359,7 @@ S_socketpair_udp (int fd[2]) {
     errno = ECONNABORTED;
   tidy_up_and_fail:
     {
-       int save_errno = errno;
+       const int save_errno = errno;
        if (sockets[0] != -1)
            PerlLIO_close(sockets[0]);
        if (sockets[1] != -1)
@@ -4720,6 +4728,11 @@ Perl_init_global_struct(pTHX)
 #  else
     plvarsp = PL_VarsPtr;
 #  endif /* PERL_GLOBAL_STRUCT_PRIVATE */
+#  undef PERLVAR
+#  undef PERLVARA
+#  undef PERLVARI
+#  undef PERLVARIC
+#  undef PERLVARISC
 #  define PERLVAR(var,type) /**/
 #  define PERLVARA(var,n,type) /**/
 #  define PERLVARI(var,type,init) plvarsp->var = init;
@@ -4770,3 +4783,12 @@ Perl_free_global_struct(pTHX_ struct perl_vars *plvarsp)
 
 #endif /* PERL_GLOBAL_STRUCT */
 
+/*
+ * Local variables:
+ * c-indentation-style: bsd
+ * c-basic-offset: 4
+ * indent-tabs-mode: t
+ * End:
+ *
+ * ex: set ts=8 sts=4 sw=4 noet:
+ */