Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #111887

Re: sizeof environment variables array = 1?

From Richard Heathfield <rjh@cpax.org.uk>
Newsgroups comp.lang.c
Subject Re: sizeof environment variables array = 1?
Date 2017-06-09 16:53 +0100
Organization Fix this later
Message-ID <oheg3n$m2f$1@dont-email.me> (permalink)
References (3 earlier) <ohbuk2$p49$1@dont-email.me> <ohc2ha$7n2$1@dont-email.me> <lnmv9ii9dd.fsf@kst-u.example.com> <oheeb7$eu4$1@dont-email.me> <b3cd0b00-fc9b-cd50-6476-b31e66c44882@verizon.net>

Show all headers | View raw


On 09/06/17 16:48, James R. Kuyper wrote:
> On 06/09/2017 11:23 AM, Richard Heathfield wrote:
>> On 08/06/17 20:10, Keith Thompson wrote:
>>> And be careful with strncpy(); it's not just a "safer" version of
>>> strcpy().
>>
>> In fact, it's not /even/ a "safer" version of strcpy().
>>
>>> (Usually the best way to be careful with it is to avoid using
>>> it.)
>>
>> Legitimate uses of strncpy are indeed rare. As you hint, however, we
>> lack a proof that they do not exist.
>
> I've cited a case that comes up frequently in my own code which Keith
> conceded was an appropriate use. Had you forgotten it,

I didn't see it. I've recently done a catch-up "Mark All As Read".

Okay, so we have the opposite - an existence proof. Fine by me. :-)

> or do you disagree?

No, of course not. But I would still maintain that the number of 
legitimate applications of strncpy is perhaps somewhat limited in 
comparison to strcpy or even memcpy.

> I have an output file format that I'm not free to modify, which contains
> fixed-length text fields. The text that should be placed in many of
> those fields is not guaranteed to be small enough to fit, in which case
> I'm permitted to truncate it. Since fixed width of the fields is
> documented, they don't need null termination, and we want to lose as
> little text as possible to truncation. Therefore, if the field is, for
> instance, 40 characters long, and the input is 50 characters long, I put
> the 40th character from the input source into field[39], rather than
> '\0'. When reading such fields, I never assume that they're null
> terminated: I read them into, for instance, the first 40 bytes of a
> 41-byte array, and then execute field[40]='\0' before passing them to
> any routine that expects null-termination.
>
> I frequently need to confirm that two different runs of my program
> displayed only the expected differences (if any) in the output file. The
> tools I have available for examining the contents of these files look at
> the entire length of the fixed-length text fields, not just the part
> before the terminating null character (if there is one). As a result, if
> I don't pay attention to what's put into the field after the terminating
> null character, it's likely to contain garbage, and most importantly,
> it's likely to contain different garbage during different runs of the
> program, producing spurious indications that the files differ.
> Therefore, if the input is 30 characters long, and the output is 40
> characters, I want the last 10 bytes to all consistently contain '\0',
> to make it easier to compare the files.

Hmmm. If you want my advice, use strncpy. I'm surprised you didn't think 
of it yourself. It's almost tailor-made for this kind of thing.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

sizeof environment variables array = 1? DFS <nospam@dfs.com> - 2017-06-08 11:13 -0400
  Re: sizeof environment variables array = 1? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2017-06-08 11:25 -0400
    Re: sizeof environment variables array = 1? Keith Thompson <kst-u@mib.org> - 2017-06-08 09:34 -0700
      Re: sizeof environment variables array = 1? DFS <nospam@dfs.com> - 2017-06-08 13:14 -0400
        Re: sizeof environment variables array = 1? jameskuyper@verizon.net - 2017-06-08 10:51 -0700
  Re: sizeof environment variables array = 1? Gareth Owen <gwowen@gmail.com> - 2017-06-08 16:56 +0100
    Re: sizeof environment variables array = 1? DFS <nospam@dfs.com> - 2017-06-08 12:18 -0400
      Re: sizeof environment variables array = 1? scott@slp53.sl.home (Scott Lurndal) - 2017-06-08 16:35 +0000
      Re: sizeof environment variables array = 1? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2017-06-08 12:43 -0400
        Re: sizeof environment variables array = 1? DFS <nospam@dfs.com> - 2017-06-08 13:49 -0400
          Re: sizeof environment variables array = 1? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-08 13:58 -0400
            Re: sizeof environment variables array = 1? DFS <nospam@dfs.com> - 2017-06-08 14:45 -0400
          Re: sizeof environment variables array = 1? Keith Thompson <kst-u@mib.org> - 2017-06-08 12:10 -0700
            Re: sizeof environment variables array = 1? DFS <nospam@dfs.com> - 2017-06-09 11:21 -0400
            Re: sizeof environment variables array = 1? Richard Heathfield <rjh@cpax.org.uk> - 2017-06-09 16:23 +0100
              Re: sizeof environment variables array = 1? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-09 11:48 -0400
                Re: sizeof environment variables array = 1? Richard Heathfield <rjh@cpax.org.uk> - 2017-06-09 16:53 +0100
                Re: sizeof environment variables array = 1? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-09 12:18 -0400
                Re: sizeof environment variables array = 1? Richard Heathfield <rjh@cpax.org.uk> - 2017-06-09 17:28 +0100
                Re: sizeof environment variables array = 1? Keith Thompson <kst-u@mib.org> - 2017-06-09 09:12 -0700
              Re: sizeof environment variables array = 1? "Patrick.Schluter" <Patrick.Schluter@free.fr> - 2017-06-10 09:28 +0200
          Re: sizeof environment variables array = 1? jak <please@nospam.tnx> - 2017-06-08 22:18 +0200
            Re: sizeof environment variables array = 1? DFS <nospam@dfs.com> - 2017-06-08 17:31 -0400
              Re: sizeof environment variables array = 1? Keith Thompson <kst-u@mib.org> - 2017-06-08 15:12 -0700
                Re: sizeof environment variables array = 1? DFS <nospam@dfs.com> - 2017-06-09 11:21 -0400
                Re: sizeof environment variables array = 1? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-09 16:39 +0100
                Re: sizeof environment variables array = 1? Gareth Owen <gwowen@gmail.com> - 2017-06-10 07:26 +0100
                Re: sizeof environment variables array = 1? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-10 13:21 +0100
                Re: sizeof environment variables array = 1? gordonb.q80bx@burditt.org (Gordon Burditt) - 2017-06-10 17:32 -0500
                Re: sizeof environment variables array = 1? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-11 01:01 +0100
                Re: sizeof environment variables array = 1? luser droog <luser.droog@gmail.com> - 2017-06-11 01:28 -0700
                Re: sizeof environment variables array = 1? raltbos@xs4all.nl (Richard Bos) - 2017-06-17 18:38 +0000
                Re: sizeof environment variables array = 1? Keith Thompson <kst-u@mib.org> - 2017-06-09 08:41 -0700
                Re: sizeof environment variables array = 1? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-09 11:50 -0400
                Re: sizeof environment variables array = 1? raltbos@xs4all.nl (Richard Bos) - 2017-06-17 18:37 +0000
                Re: sizeof environment variables array = 1? DFS <nospam@dfs.com> - 2017-06-18 09:11 -0400
          Re: sizeof environment variables array = 1? "Patrick.Schluter" <Patrick.Schluter@free.fr> - 2017-06-10 09:19 +0200
      Re: sizeof environment variables array = 1? Robert Wessel <robertwessel2@yahoo.com> - 2017-06-08 12:44 -0500
    Re: sizeof environment variables array = 1? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-08 18:01 +0100
      Re: sizeof environment variables array = 1? Gareth Owen <gwowen@gmail.com> - 2017-06-08 18:45 +0100
    Re: sizeof environment variables array = 1? Keith Thompson <kst-u@mib.org> - 2017-06-08 11:51 -0700

csiph-web