Menu

#214 [PIC16] strncpy doesn't follow ANSI standard

None
closed-accepted
PIC16 (1)
5
2014-12-13
2013-12-26
No

strncpy in libc for PIC16 didn't follow this ANSI standard requirement:

If the length of src is less than n, strncpy() writes additional null bytes to dest to ensure that a total of n bytes are written.

This patch fixes it. Now, PIC16 regression tests pass bug-2231 which addresses the same bug for z80 port.

1 Attachments

Discussion

  • Maarten Brock

    Maarten Brock - 2013-12-26

    Sorry, Diego, I do not mean to disappoint you because I really appreciate your help, but...

    Can anyone give me a good reason why pic16 needs its own version of the library functions, including this one? If not, can we please discard them?

     
  • Philipp Klaus Krause

    AFAIR, I asked the same a few years ago on sdcc-devel. AFAIR, the reply back then was that the pic ports are so broken, they can't compile the versions most other ports use.

    Nevertheless I think, we should have only a few well-justified port-specific functions, and mostly just use the same versions for all ports.
    If someone wants to do it, we could probably just make the pic16 use the generic library implementations. If there's no one, we might want to postpone it until after the relase (and use Diego's patch for the release).

    Philipp

     
  • Maarten Brock

    Maarten Brock - 2013-12-27

    Ok, that sounds fine.

    Still, Diego can you check the difference in generated output between your version and the one in device/lib/_strncpy.c? You use d and d1 in reverse. I'd even be interested in the difference on other targets. Preferably we'd use identical versions from now on. Or document why one is better than the other in the comments.

    Maarten

     
  • Diego Herranz

    Diego Herranz - 2013-12-27

    I attach the comparison of device/lib/_strncpy.c and a modified version swapping d and d1, both compiled for pic16.

    The one in device/lib/_strncpy.c has 1 instruction less (2 words).

    I don't know about other ports.

     
  • Maarten Brock

    Maarten Brock - 2013-12-27

    Ok, this is totally weird. The version used for mcs51 and z80 is slightly smaller when used for pic16 and your pic16 version is much smaller when used for mcs51 and z80. Only hc08 is efficient when using the current implementation for it. But when I change the code like below, only hc08 gets more efficient.

    char *strncpy ( char * d, const char * s, size_t n )
    {
      register char * d1 =  d;
      register char c;
    
      while ( n && (c = *s++) )
      {
        n-- ;
        *d1++ = c ;
      }
      while ( n )
      {
        n-- ;
        *d1++ = '\0' ;
      }
      return d;
    }
    
     

    Last edit: Maarten Brock 2013-12-27
  • Diego Herranz

    Diego Herranz - 2014-10-22

    Because probably nobody has time to switch PIC16 port to use standard libraries, should we apply this patch (or similar) for the time being and open a ticket like "[PIC16] Use standard library functions"?

     
  • Philipp Klaus Krause

    Applied in revision #9133.

    Philipp

     
  • Philipp Klaus Krause

    • status: open --> closed-accepted
    • assigned_to: Philipp Klaus Krause
    • Group: -->
     

Log in to post a comment.