How long is a piece of string?



  • Just found this in some old code at work

     szMaskedReferenceField[ strlen( szMaskedReferenceField ) + 1 ] = 0;

     

    One way to ensure that it's null-terminated at the null terminator, I suppose.

     

     



  • Wait, are you sure about that?  If you had the char *mystring="abc", it would have a strlength of 3 and array length of 4.  An index of 3+1 would be out of the array's bounds (assuming this is C/C++ or some other sane zero-based array language).  mystring[strlen(mystring)] == mystring[3] would be already set to \0, but strlen() +1 is a buffer overflow...



  • @Xyro said:

    Wait, are you sure about that?  If you had the char *mystring="abc", it would have a strlength of 3 and array length of 4.  An index of 3+1 would be out of the array's bounds (assuming this is C/C++ or some other sane zero-based array language).  mystring[strlen(mystring)] == mystring[3] would be already set to \0, but strlen() +1 is a buffer overflow...

    yeah, but if you had:

    char * message_buffer = (char 😉 malloc ( MESSAGE_BUFFER_SIZE * sizeof(char) );

    and a shorter message in the buffer, you could null terminate it up to the buffer size without overflow ...



  • @Xyro said:

    Wait, are you sure about that?  If you had the char *mystring="abc", it would have a strlength of 3 and array length of 4.  An index of 3+1 would be out of the array's bounds (assuming this is C/C++ or some other sane zero-based array language).  mystring[strlen(mystring)] == mystring[3] would be already set to \0, but strlen() +1 is a buffer overflow...

    Perhaps it's a double-null-terminated string? I somehow doubt it though...



  • @Xyro said:

    Wait, are you sure about that?  If you had the char *mystring="abc", it would have a strlength of 3 and array length of 4.  An index of 3+1 would be out of the array's bounds (assuming this is C/C++ or some other sane zero-based array language).  mystring[strlen(mystring)] == mystring[3] would be already set to \0, but strlen() +1 is a buffer overflow...
    This code is useful for making sure your buffer overflows don't overflow.



  • @Nelle said:

    char * message_buffer = (char ) malloc ( MESSAGE_BUFFER_SIZE sizeof(char) );




    Stop it.



    If you're using C, you don't need either of those. The first hides warnings, and the second is superfluous.



    If you're using C++ you should be using new (and probably shouldn't be using char arrays.)



  • @PJH said:

    Perhaps it's a double-null-terminated string? I somehow doubt it though...
    WTF.

    (re-reads post)  WTF WTF.

    Never in my wildest nightmares would I dream up something so demented.  "Why do we even have double-null-terminated strings at all?
    Why not just pass an array of pointers to strings?
    "  I can't believe that guy tries to justify this abomination's existence...



  • @PJH said:

    Stop it.



    If you're using C, you don't need either of those. The first hides warnings, and the second is superfluous.

    im paid per kB of code.



  • @Xyro said:

    @PJH said:

    Perhaps it's a double-null-terminated string? I somehow doubt it though...
    WTF.

    (re-reads post)  WTF WTF.

    Never in my wildest nightmares would I dream up something so demented.

    You know what language could really use something like this?  Java.

    public class DoubleNullString {
      private String _str;
    
      public DoubleNullString() {
        _str = "";
      }
    
      public DoubleNullString(String [] strs)
      {
        _str = "";
        for (int i = 0; i < strs.length; ++i) {
          _str = _str + strs[i] + ((char)0);
        }
      }
    
      public int size() {
        int count = 0;
        int idx = _str.indexOf(0);
        while (idx > 0) {
          ++count;
          idx = _str.indexOf(0, idx + 1);
        }
        return count;
      }
    
      public String get(int idx) {
        if (idx < 0)
          throw new IndexOutOfBoundsException(Integer.toString(idx) + " < 0");
        int start = 0;
        int end = _str.indexOf(0);
        String val;
        for (int i = 0; i < idx; ++i) {
          start = end + 1;
          end = _str.indexOf(0, end + 1);
          if (end < 0)
            throw new IndexOutOfBoundsException(Integer.toString(idx) + " > size");
        }
        return _str.substring(start, end);
      }
    
      public void put(int idx, String str) {
        if (idx < 0)
          throw new IndexOutOfBoundsException(Integer.toString(idx) + " < 0");
        int size = size();
        if (idx > size) {
          for (int i = 0; i < (idx - size); ++i)
            _str = _str + ((char)0);
          _str = _str + str + ((char)0);
        }
        else {
          int start = 0;
          int end = _str.indexOf(0);
          String val;
          for (int i = 0; i < idx; ++i) {
            start = end + 1;
            end = _str.indexOf(0, end + 1);
          }
          _str = _str.substring(0, start) + str + _str.substring(end);
        }
        return;
      }
    
      public void append(String str) {
        _str = _str + str + ((char)0);
        return;
      }
    
      public java.util.List<String> toList() {
        return java.util.Arrays.asList(_str.split(Character.toString((char)0), -1));
      }
        
    }
    

Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.