~vcs-imports/gawk/master

« back to all changes in this revision

Viewing changes to builtin.c

  • Committer: Arnold D. Robbins
  • Date: 2016-11-18 04:00:17 UTC
  • mto: (408.31.43)
  • mto: This revision was merged to the branch mainline in revision 727.
  • Revision ID: git-v1:e8b0cf14d975304166c58a2d04a2943ab821367a
Audit use of stptr for NUL termination. Update doc before merge to master.

Show diffs side-by-side

added added

removed removed

Lines of Context:
135
135
 
136
136
 
137
137
        /* otherwise die verbosely */
138
 
        if ((rp != NULL) ? is_non_fatal_redirect(rp->value) : is_non_fatal_std(fp))
 
138
        if ((rp != NULL) ? is_non_fatal_redirect(rp->value, strlen(rp->value)) : is_non_fatal_std(fp))
139
139
                update_ERRNO_int(errno);
140
140
        else
141
141
                fatal(_("%s to \"%s\" failed (%s)"), from,
194
194
        FILE *fp;
195
195
        int status = 0;
196
196
        const char *file;
 
197
        int len;
197
198
 
198
199
        /*
199
200
         * November, 2012.
220
221
 
221
222
        tmp = POP_STRING();
222
223
        file = tmp->stptr;
 
224
        len = tmp->stlen;
223
225
 
224
226
        /* fflush("") */
225
227
        if (tmp->stlen == 0) {
234
236
        if (rp != NULL) {
235
237
                if ((rp->flag & (RED_WRITE|RED_APPEND)) == 0) {
236
238
                        if ((rp->flag & RED_PIPE) != 0)
237
 
                                warning(_("fflush: cannot flush: pipe `%s' opened for reading, not writing"),
238
 
                                        file);
 
239
                                warning(_("fflush: cannot flush: pipe `%.*s' opened for reading, not writing"),
 
240
                                        len, file);
239
241
                        else
240
 
                                warning(_("fflush: cannot flush: file `%s' opened for reading, not writing"),
241
 
                                        file);
 
242
                                warning(_("fflush: cannot flush: file `%.*s' opened for reading, not writing"),
 
243
                                        len, file);
242
244
                        DEREF(tmp);
243
245
                        return make_number((AWKNUM) status);
244
246
                }
246
248
                if (fp != NULL)
247
249
                        status = rp->output.gawk_fflush(fp, rp->output.opaque);
248
250
                else if ((rp->flag & RED_TWOWAY) != 0)
249
 
                                warning(_("fflush: cannot flush: two-way pipe `%s' has closed write end"),
250
 
                                        file);
 
251
                                warning(_("fflush: cannot flush: two-way pipe `%.*s' has closed write end"),
 
252
                                        len, file);
251
253
        } else if ((fp = stdfile(tmp->stptr, tmp->stlen)) != NULL) {
252
254
                status = fflush(fp);
253
255
        } else {
254
256
                status = -1;
255
 
                warning(_("fflush: `%s' is not an open file, pipe or co-process"), file);
 
257
                warning(_("fflush: `%.*s' is not an open file, pipe or co-process"), len, file);
256
258
        }
257
259
        DEREF(tmp);
258
260
        return make_number((AWKNUM) status);
1685
1687
                rp = redirect(redir_exp, redirtype, & errflg, true);
1686
1688
                if (rp != NULL) {
1687
1689
                        if ((rp->flag & RED_TWOWAY) != 0 && rp->output.fp == NULL) {
1688
 
                                if (is_non_fatal_redirect(redir_exp->stptr)) {
 
1690
                                if (is_non_fatal_redirect(redir_exp->stptr, redir_exp->stlen)) {
1689
1691
                                        update_ERRNO_int(EBADF);
1690
1692
                                        return;
1691
1693
                                }
2169
2171
                rp = redirect(redir_exp, redirtype, & errflg, true);
2170
2172
                if (rp != NULL) {
2171
2173
                        if ((rp->flag & RED_TWOWAY) != 0 && rp->output.fp == NULL) {
2172
 
                                if (is_non_fatal_redirect(redir_exp->stptr)) {
 
2174
                                if (is_non_fatal_redirect(redir_exp->stptr, redir_exp->stlen)) {
2173
2175
                                        update_ERRNO_int(EBADF);
2174
2176
                                        return;
2175
2177
                                }
2243
2245
                rp = redirect(redir_exp, redirtype, & errflg, true);
2244
2246
                if (rp != NULL) {
2245
2247
                        if ((rp->flag & RED_TWOWAY) != 0 && rp->output.fp == NULL) {
2246
 
                                if (is_non_fatal_redirect(redir_exp->stptr)) {
 
2248
                                if (is_non_fatal_redirect(redir_exp->stptr, redir_exp->stlen)) {
2247
2249
                                        update_ERRNO_int(EBADF);
2248
2250
                                        return;
2249
2251
                                }
3679
3681
                char *category;
3680
3682
                int lc_cat = -1;
3681
3683
 
 
3684
                char save = t->stptr[t->stlen];
 
3685
                t->stptr[t->stlen] = '\0';
3682
3686
                category = t->stptr;
3683
3687
 
3684
3688
                /* binary search the table */
3697
3701
                                break;
3698
3702
                        }
3699
3703
                }
 
3704
                t->stptr[t->stlen] = save;
3700
3705
                if (lc_cat == -1)       /* not there */
3701
3706
                        fatal(_("dcgettext: `%s' is not a valid locale category"), category);
3702
3707
 
3725
3730
#if ENABLE_NLS && defined(LC_MESSAGES) && HAVE_DCGETTEXT
3726
3731
        int lc_cat;
3727
3732
        char *domain;
 
3733
        char save;
 
3734
        bool saved_end = false;
3728
3735
 
3729
3736
        if (nargs == 3) {       /* third argument */
3730
3737
                tmp = POP_STRING();
3736
3743
        if (nargs >= 2) {  /* second argument */
3737
3744
                t2 = POP_STRING();
3738
3745
                domain = t2->stptr;
 
3746
                save = domain[t2->stlen];
 
3747
                domain[t2->stlen] = '\0';
 
3748
                saved_end = true;
3739
3749
        } else
3740
3750
                domain = TEXTDOMAIN;
3741
3751
#else
3754
3764
 
3755
3765
#if ENABLE_NLS && defined(LC_MESSAGES) && HAVE_DCGETTEXT
3756
3766
        the_result = dcgettext(domain, string, lc_cat);
 
3767
        if (saved_end)
 
3768
                domain[t2->stlen] = save;
3757
3769
        if (t2 != NULL)
3758
3770
                DEREF(t2);
3759
3771
#else
3776
3788
#if ENABLE_NLS && defined(LC_MESSAGES) && HAVE_DCGETTEXT
3777
3789
        int lc_cat;
3778
3790
        char *domain;
 
3791
        char save;
 
3792
        bool saved_end = false;
3779
3793
 
3780
3794
        if (nargs == 5) {       /* fifth argument */
3781
3795
                tmp = POP_STRING();
3788
3802
        if (nargs >= 4) {       /* fourth argument */
3789
3803
                t3 = POP_STRING();
3790
3804
                domain = t3->stptr;
 
3805
                save = domain[t3->stlen];
 
3806
                domain[t3->stlen] = '\0';
 
3807
                saved_end = true;
3791
3808
        } else
3792
3809
                domain = TEXTDOMAIN;
3793
3810
#else
3814
3831
#if ENABLE_NLS && defined(LC_MESSAGES) && HAVE_DCGETTEXT
3815
3832
 
3816
3833
        the_result = dcngettext(domain, string1, string2, number, lc_cat);
 
3834
        if (saved_end)
 
3835
                domain[t3->stlen] = save;
3817
3836
        if (t3 != NULL)
3818
3837
                DEREF(t3);
3819
3838
#else
3846
3865
        /* set defaults */
3847
3866
        directory = NULL;
3848
3867
        domain = TEXTDOMAIN;
 
3868
        char save;
 
3869
        bool saved_end = false;
3849
3870
 
3850
3871
        if (nargs == 2) {       /* second argument */
3851
3872
                t2 = POP_STRING();
3852
3873
                domain = (const char *) t2->stptr;
 
3874
                save = t2->stptr[t2->stlen];
 
3875
                t2->stptr[t2->stlen] = '\0';
 
3876
                saved_end = true;
3853
3877
        }
3854
3878
 
3855
3879
        /* first argument */
3860
3884
        the_result = bindtextdomain(domain, directory);
3861
3885
 
3862
3886
        DEREF(t1);
 
3887
        if (saved_end)
 
3888
                t2->stptr[t2->stlen] = save;
3863
3889
        if (t2 != NULL)
3864
3890
                DEREF(t2);
3865
3891