[haiku-commits] Re: r40796 - in haiku/trunk/src: apps/codycam apps/deskbar kits/tracker

  • From: Ingo Weinhold <ingo_weinhold@xxxxxx>
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Fri, 04 Mar 2011 01:53:12 +0100

On 2011-03-03 at 21:58:59 [+0100], jonas@xxxxxxxxxxx wrote:
[...]
> +status_t
> +GetLocalizedFileName(entry_ref& ref, BString& localizedFileName, bool 
> traverse)
> +{
> +    // Looks up a localized filename, by reading a catalog signature,
> +    // context and string to be translated, from an attribute on the
> +    // entry_ref, and using these to look up a translation in the catalog
> +    // of the top preferred language.
> +
> +    // Attribute format:  "signature:context:string"
> +    // (no colon in any of signature, context and string)
> +
> +    // It fails when a comment is present in the catalog.
> +
> +    status_t status;

Coding style: Move declaration down to first use, which is ...

> +
> +    BEntry entry(&ref, traverse);
> +    if (!entry.Exists())
> +        return B_ENTRY_NOT_FOUND;
> +
> +    BNode node(&entry);
> +    status = node.InitCheck();

... here.

> +    if (status != B_OK)
> +        return status;
> +
> +    attr_info attr;
> +    ssize_t bytes = 0;

Move down to first use, which is ...

> +
> +    status = node.GetAttrInfo("SYS:NAME", &attr);
> +    if (status != B_OK)
> +        return status;
> +
> +    char attribute[attr.size + 1];
> +    bytes = node.ReadAttr("SYS:NAME", B_MIME_TYPE, 0, &attribute, 
> attr.size);

... here.

> +
> +    if (bytes < 0)
> +        return bytes;
> +    
> +    if (bytes == 0 || bytes != attr.size)
> +        return B_ENTRY_NOT_FOUND;
> +
> +    attribute[bytes] = '\0';
> +
> +    char* signature = attribute;
> +    char* context = NULL;
> +    char* string = NULL;

Move down to first use.

> +
> +    ssize_t i = 0;
> +    for (; i < bytes; i++) {
> +        if (signature[i] == ':') {
> +            signature[i] = '\0';
> +            context = &signature[i + 1];
> +            break;
> +        }
> +    }
> +
> +    for (; i < bytes; i++) {
> +        if (signature[i] == ':') {
> +            signature[i] = '\0';
> +            string = &signature[i + 1];
> +            break;
> +        }
> +    }

You have hand-implemented the strchr() function [1] twice.

> +
> +    BCatalog catalog(signature);
> +
> +    const char* temp = catalog.GetString(string, context);

Not that it crashes or anything, but string (and context) could be NULL at 
this point, which, programming defensively, should be caught before.

> +    if (temp == NULL)
> +        return B_ENTRY_NOT_FOUND;
> +
> +    localizedFileName = temp;
> +    return B_OK;
> +}

CU, Ingo

[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/strchr.html

Other related posts: