Closed Bug 58221 Opened 25 years ago Closed 1 year ago

don't use strlen to check if a string is of length 0

Categories

(Core :: General, defect, P3)

defect

Tracking

()

RESOLVED INACTIVE

People

(Reporter: sspitzer, Unassigned)

References

Details

(Keywords: perf)

Attachments

(9 obsolete files)

from an email from mark stankus, Trivial improvement to performance: file:nsMsgFolderCache.cpp sspitzer 1.25 lines: (sept 7, 2000 11:15 pm) 387 if (nsCRT::strlen(pathKey) == 0) { 388 return NS_ERROR_FAILURE; 389 } slightly more efficient: 387 if (*pathKey=='\0') { 388 return NS_ERROR_FAILURE; 389 } logging a bug about this so I remember to check the rest of the code for stuff like this. thanks mark.
seth, is this bug still useful?
Severity: normal → trivial
r=hwaara
Keywords: approval, patch
QA Contact: esther → stephend
Depends on: 71874
Keywords: perf
No longer depends on: 71874
Blocks: 71874
Currently, in the following files (nsCRT::)strlen(foo) is compared with 0: addrbook/src/nsAbAddressCollecter.cpp addrbook/src/nsAddrDatabase.cpp addrbook/src/nsDirPrefs.cpp base/search/src/nsMsgSearchAdapter.cpp base/search/src/nsMsgSearchTerm.cpp base/src/nsMessengerMigrator.cpp base/src/nsMsgAccountManager.cpp base/src/nsMsgFolderCache.cpp base/util/nsMsgIdentity.cpp base/util/nsMsgIncomingServer.cpp base/util/nsMsgFolder.cpp compose/src/nsMsgCopy.cpp compose/src/nsMsgCompose.cpp compose/src/nsSmtpProtocol.cpp compose/src/nsSmtpService.cpp db/msgdb/src/nsMsgDatabase.cpp db/msgdb/src/nsMsgHdr.cpp imap/src/nsIMAPNamespace.cpp imap/src/nsImapProtocol.cpp imap/src/nsImapService.cpp imap/src/nsImapUrl.cpp imap/src/nsImapMailFolder.cpp imap/src/nsImapIncomingServer.cpp import/outlook/src/nsOutlookMail.cpp local/src/nsLocalMailFolder.cpp local/src/nsMovemailService.cpp mime/cthandlers/vcard/mimevcrd.cpp mime/src/mimei.cpp mime/src/mimetpfl.cpp mime/src/mimetpla.cpp news/src/nsNewsFolder.cpp news/src/nsNntpIncomingServer.cpp Few more files use strlen ineffisiently (e.g. nsCRT::strlen of const string, multiple strlen on same string etc.)
removing myself from cc:
Attached patch strlen removal patch (obsolete) — Splinter Review
This patch removes uses of {PL_,nsCmd::,}strlen all over the tree where its output is compared to 0. The function calls are replaced with comparisons between the 0th character of a string and the NULL character.
Attached patch applies to trunk (obsolete) — Splinter Review
rediffed
Attachment #97916 - Attachment is obsolete: true
Attached patch another rediff (obsolete) — Splinter Review
Attachment #100754 - Attachment is obsolete: true
OK. Based on a quick skim: 1) Don't do the comparison to '\0'. Just test *foo as a boolean; any platform on which '\0' does not evaluate to false we don't run on anyway. content/xml/document/src/nsXMLDocument.cpp -- that code looks fucked if GetLocalizedUnicharPref ever fails... want to fix that while you're in there? (a "no" is an acceptable answer if you'd really rather not, but then file a bug on me). > + if((nsnull == valueInUTF8) || *valueInUTF8 == '\0') if ( !valueInUTF8 || !*valueInUTF8) -- simplicity all over. ;) > + if( (mPrintFile == nsnull) || *mPrintFile == '\0' ) same > + if( (s != NULL) && *s != '\0') same. > + if (*((const char *) arbitraryHeaderTerm) != '\0') arbitraryHeaderTerm is an nsXPIDLCString. Those have a convenient IsEmpty() method that returns a boolean. Use that, please, instead of this casting stuff (yes, I know the existing code did it....) > + if (!(const char*) username || *((const char*)username) == '\0') { Again, IsEmpty() > + *defaultServerKey != 0) { And again. > + PRBool redirectLogon = ((const char *) redirectorType && *((const char *) redirectorType) != '\0'); Here too. > + *((const char*) password) == '\0') And here. > + if (! (onlineName.get()) || *(onlineName.get()) == '\0' And here > + if ((const char*) onlineName && *((const char *) onlineName) != '\0') And here. ;) > + if (NS_SUCCEEDED(rv) && *oldMsgId != '\0') And here. > + if ((const char *)onlineName == nsnull || *((const char *) onlineName) == '\0') And here. > + if ((const char *) folderName && *folderName != 0) And here. In fact, I'll stop commenting on those now, especially since in many places the patch does not cover enough of the file (diff -u6 next time please?) to see what the variable is declared as. Just make sure all users of nsCAutoString or nsXPIDLCString are using IsEmpty() instead of getting out a char* and playing with it. > + if((szText == NULL) || *szText == '\0') Fix the first part of that to not suck too? > + if (*PREF_FILE_NAME_IN_4x == '\0') { Make sure that macro never expands to anything that would require parens around it? Or just add the parens?
Attached patch -u10 should be up to review (obsolete) — Splinter Review
i'll look to see if i can find any newly introduced offenders...
Attachment #102297 - Attachment is obsolete: true
Attached patch latest diff (obsolete) — Splinter Review
I searched the tree for new instances of this construct. This patch by timeless fixes all of them, except some in sample plugins. I modified the patch so it would actually compile.
Comment on attachment 102406 [details] [diff] [review] latest diff My apologies for the comments being slightly out of patch order... > - if(nsCRT::strlen(detector_name) > 0) { > + if(*detector_name) { > - if(nsCRT::strlen(detector_name) > 0) { > + if(*detector_name) { (2 different places). nsCRT::strlen is null-safe... this would crash, imo. > - if (nsCRT::strlen(aUStr) == 0) { > + if (!*aUStr) { > - if (nsCRT::strlen(aUStr) == 0) { > + if (!*aUStr) { (2 places) Can aUStr be null? (diff -u6 next time, please!) > + if (!*buf || buf[0] == '!') This is looking at the exact same byte on both sides of the ||; how about addressing them consistently? ;) > - if (strlen(lts->cookie) > 0) { > + if (*lts->cookie) { if (ltermSendChar(lts, lts->cookie, strlen(lts->cookie)) != 0) Wanna fix that last arg as well? It should probably be "*lts->cookie != '\0'" (need the comparison so we're passing a valid PRBool value). > value = defaultValue ? nsCRT::strdup(defaultValue) : nsnull; > } >- if (PL_strlen(value) == 0) >+ if (!*value) That's gonna crash if defaultValue is false... PL_strlen is null-safe and returns 0 for null, as it happens. > - if (PL_strlen (server->serverName) == 0) > + if (!*server->serverName) Similar issue here. > - if (PL_strlen(*aPassword) > 0) > + if (**aPassword) And here. > - if (PL_strlen(*aPassword) > 0) { > + > + if (**aPassword) { You know the drill. ;) > - if (PL_strlen(*aUsername) > 0) > + > + if (**aUsername) Again. > - if (PL_strlen(m_prefix) == 0) > + if (!*m_prefix) And here. > - Recycle(valueInUTF8); > + if(!valueInUTF8) > return; > + if(!*valueInUTF8) > + { > + Recycle(valueInUTF8); What's with the whitespace? Please use tabs or spaces consistently with the existing code. > + if (!valueInUTF8) > + {} > + else if (!*valueInUTF8) how about: if (valueInUTF8) { if (!*valueInUTF8) { Recycle(valueInUTF8); } else { // existing code } } (and reindent the existing code, of course). > - if (ns && (PL_strlen(ns->GetPrefix()) == 0) && > + if (ns && !*(ns->GetPrefix()) && Null-check ns->GetPrefix()? (store in a temporary) > - nsCRT::strlen(buffer) == 0 && > + !*buffer && nsCRT::strlen is null-safe... Check whether that matters? > + if(!filename || !*filename|| (strlen(filename) > _MAX_PATH)) Space after *filename? > PRUnichar *result = nsnull; > if (NS_SUCCEEDED(rv = gTextToSubURI->UnEscapeAndConvert(mEncoding.get(), filename.get(), &result)) && (result)) { >- if (nsCRT::strlen(result) > 0) { >+ if (*result) { Could you just switch this code to nsXPIDLString? Or is it passing "result" out to someone? > + if ((*temp == '#') || !*temp) remove the extraneous parens > - if (!tPath || PL_strlen(tPath) == 0) > + if (!tPath || !*tPath) Don't bother with that change; that code will not exist once the tree reopens. ;) > - if (0<strlen(key) && strncmp(key, inKey, strlen(inKey)) == 0) > + if (*key && strncmp(key, inKey, strlen(inKey)) == 0) { > if(key) > DisposePtr(key); This code assumes strlen() and strncmp() are null-safe (and your change assumes key != NULL. Are these valid assumptions?
Attachment #102406 - Flags: needs-work+
Oh, and I will try to be much quicker with the next review, now that I have read the whole patch.
Attached patch update (obsolete) — Splinter Review
This should address all of your comments. I reviewed the safety of assuming pointers are non-null in all places where your comments suggested it.
Attachment #102406 - Attachment is obsolete: true
- if (docURLStrPS && nsCRT::strlen(docURLStrPS) > 0) { + if (docURLStrPS && *docURLStrPS) { What happened to the indentation here? tabs? + if (!*valueInUTF8) Recycle (valueInUTF8); This is weirdly indented too.... + char *prefix = ns->GetPrefix(); + if (ns && (!prefix || !*prefix) && What if "ns" is null? GetPrefix() actually gets inlined and is cheap, so I'd be OK with just calling it twice, though I'm not sure whether *(ns->GetPrefix()) would do the right thing (like compile)... Failing all else, just leave this one be for now. In security/nss/cmd/certutil/certutil.c: + if (*buffer != 0) { drop the "!= 0" part. sr=bzbatsky with those four changes.
Comment on attachment 105444 [details] [diff] [review] update r=timeless
Attachment #105444 - Flags: review+
cc dmose (sorry for the spam)
What is the status of this bug? Is the patch still working or does it need to be updated? Who is willing to sr= it?
Were the portions of the patch applying to security/ applied?
Ah. Not all of them. The security/manager ones were, but NSS is restricted. Over to NSS to land this.
Assignee: sspitzer → wtc
Component: Mail Back End → Libraries
Product: MailNews → NSS
QA Contact: stephend → bishakhabanerjee
Version: Trunk → 3.0
Attached patch nss (obsolete) — Splinter Review
Attachment #102360 - Attachment is obsolete: true
Attachment #105444 - Attachment is obsolete: true
Attached patch directory (obsolete) — Splinter Review
as for mailnews, there's one file left in my checkin directory: mailnews/imap/src/nsImapUrl.cpp It had merge conflicts or something which probably forced me to omit it. Or perhaps i wanted to land it with some other bug (the relevant function uses PR_Malloc w/ nsCRT::Free and doesn't complain about OOM).
I will take care of the NSS patch. Assigned the bug back.
Assignee: wtc → sspitzer
Component: Libraries → Mail Back End
Product: NSS → MailNews
QA Contact: bishakhabanerjee → stephend
Version: 3.0 → Trunk
Comment on attachment 117634 [details] [diff] [review] directory needs checkin
Attachment #117634 - Flags: review?(dmose)
Comment on attachment 117634 [details] [diff] [review] directory directory patch checked in r+sr=dmose on both trunk and client branch
Attachment #117634 - Attachment is obsolete: true
Attachment #117634 - Flags: review?(dmose) → review+
wtc: what's the status of the nss patch?
Attachment #117633 - Flags: review?(wchang0222)
Product: MailNews → Core
sorry for the spam. making bugzilla reflect reality as I'm not working on these bugs. filter on FOOBARCHEESE to remove these in bulk.
Assignee: sspitzer → nobody
Comment on attachment 117633 [details] [diff] [review] nss Looking at the current NSS trunk, this patch is completely obsolete. 3 of the 4 hunks no longer exist and the 4th already makes the change the patch is proposing.
Attachment #117633 - Attachment is obsolete: true
Attachment #117633 - Flags: review?(wtc)
Filter on "Nobody_NScomTLD_20080620"
QA Contact: stephend → backend
Product: Core → MailNews Core
(In reply to comment #30) > With attachment 117633 [details] [diff] [review] no longer relevant, the following appear to still be > uses this bug refers to. However, I'm not entirely certain that's the case. http://mxr.mozilla.org/mozilla/source/modules/softupdt/src/nsInstallFile.cpp#161 http://mxr.mozilla.org/mozilla/source/content/svg/content/src/nsSVGTransformListParser.cpp#126 These two are still relevant IMHO, which makes this a core bug, not a MailNews one (at a very quick glance, I also couldn't see any specific mailnews cases).
Component: Backend → General
Product: MailNews Core → Core
QA Contact: backend → general
(In reply to Mark Banner (:standard8) from comment #32) > http://mxr.mozilla.org/mozilla/source/modules/softupdt/src/nsInstallFile. > cpp#161 > http://mxr.mozilla.org/mozilla/source/content/svg/content/src/ > nsSVGTransformListParser.cpp#126 I can't find those in dxr.lanedo.com
Severity: trivial → S4
Assignee: nobody → grover7677
Status: NEW → ASSIGNED
Attachment #9341410 - Attachment description: Bug 58221 - Change strlen(foo) == 0 to *foo == '\0\ r?#necko-reviewers → Bug 58221 - Change strlen(foo) == 0 to *foo == '\0\' r?#necko-reviewers

Comment on attachment 9341410 [details]
Bug 58221 - Change strlen(foo) == 0 to *foo == '\0' r?#necko-reviewers

Revision D182285 was moved to bug 1840882. Setting attachment 9341410 [details] to obsolete.

Attachment #9341410 - Attachment is obsolete: true

The bug assignee is inactive on Bugzilla, so the assignee is being reset.

Assignee: grover7677 → nobody
Status: ASSIGNED → NEW

no activity for a while and probably fixed already

Status: NEW → RESOLVED
Closed: 1 year ago
Resolution: --- → INACTIVE
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: