Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 371141 - sys-apps/openrc: $IFS misuse in unmount-related initscripts (mount-ro and localmount)
Summary: sys-apps/openrc: $IFS misuse in unmount-related initscripts (mount-ro and loc...
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Hosted Projects
Classification: Unclassified
Component: OpenRC (show other bugs)
Hardware: All Linux
: Normal minor (vote)
Assignee: OpenRC Team
URL:
Whiteboard:
Keywords: InVCS
Depends on:
Blocks: 374183
  Show dependency tree
 
Reported: 2011-06-11 13:02 UTC by Kirill Elagin
Modified: 2011-07-07 19:56 UTC (History)
2 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
Fix (0001-Fix-misuse-of-IFS.patch,1.53 KB, patch)
2011-06-11 13:02 UTC, Kirill Elagin
Details | Diff
Old and new ways of setting/unsetting IFS to OIFS (testifs.sh,1.09 KB, text/plain)
2011-06-27 19:47 UTC, Anthony Basile
Details
Fix saving and restoring of $IFS (0001-Fix-saving-and-restoring-of-IFS.patch,1.53 KB, patch)
2011-06-27 20:20 UTC, Kirill Elagin
Details | Diff
0001-save-and-restore-IFS-correctly.patch (0001-save-and-restore-IFS-correctly.patch,1.55 KB, text/plain)
2011-07-07 16:55 UTC, William Hubbs
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Kirill Elagin 2011-06-11 13:02:55 UTC
Created attachment 276649 [details, diff]
Fix

mount-ro and localmount initscripts use some weird logic when dealing with $IFS variable (reading list of mountpoints to omit).
Comment 1 Anthony Basile gentoo-dev 2011-06-11 20:18:50 UTC
I see what you did, but I'm confused at what issue you're addressing?  Can you point out what was bad with the original?
Comment 2 Kirill Elagin 2011-06-11 20:32:22 UTC
As you can see branches of the if are swapped.

The idea of this snippet is to save the value of $IFS before altering it and restore it later (“restore” means assign original value if it was defined and unset otherwise). However in the original code $IFS is UNSET if it was NOT defined.
Comment 3 Kirill Elagin 2011-06-11 20:35:01 UTC
I also added “local” to vars definition in the first file, and removed no-op assignment from the second one.
Comment 4 William Hubbs gentoo-dev 2011-06-24 17:27:34 UTC
I made the variables local that you suggested in commit 2050e67.
However, I do not follow why we need the rest of the patch. Does the
code not work correctly? If so why doesn't it?
Comment 5 Kirill Elagin 2011-06-24 18:49:36 UTC
See comment #2
> As you can see branches of the if are swapped.
> 
> The idea of this snippet is to save the value of $IFS before altering it and
> restore it later (“restore” means assign original value if it was defined and
> unset otherwise). However in the original code $IFS is UNSET if it was NOT
> defined.

Again.

What we want:
1) Store $IFS value somewhere.
2) Modify $IFS as we need to process data.
3) Restore original value.

How do we achieve this.
Store:
1) $OIFS = $IFS
2) $SIFS = 'n' if $IFS was NOT set
Restore:
1) if $SIFS == 'n' // $IFS was not set
     unset $IFS
   else
     $IFS = $OIFS

What it does now.
Store:
1) $OIFS = $IFS
2) $SIFS = 'y' if $IFS was NOT set
Restore:
1) if $SIFS == 'y' // $IFS was not set
     $IFS = $OIFS
   else // $IFS WAS set
     unset $IFS
Comment 6 Anthony Basile gentoo-dev 2011-06-27 19:47:23 UTC
Created attachment 278383 [details]
Old and new ways of setting/unsetting IFS to OIFS

I'm having trouble getting my brain around this logic clearly so I wrote the above code to test both ways and to be honest, both the old and new way seem to be doing the same thing.

Am I missing something here, or is this really just a stylistic change?

Anyhow, I changed "IFS" to X to avoid colliding with the real IFS just in case.
Comment 7 Kirill Elagin 2011-06-27 20:20:51 UTC
Created attachment 278391 [details, diff]
Fix saving and restoring of $IFS

Oh, God! Now I get what that strange “local X=$X” stands for! I thought it's just a mistake. Not enough practice in bash programming :(.

Anyway, there was no “local IFS=$IFS” in localmount and it DID NOT work. Just remove “local X=$X” from your test and you'll see it.

With local variables there is no need in all those dances with SIFS and OIFS.

New patch introduces a stylistic change in mount-ro, but also fixes a real bug in localmount.
Comment 8 Anthony Basile gentoo-dev 2011-06-27 21:12:02 UTC
(In reply to comment #7)
> Created attachment 278391 [details, diff]
> Fix saving and restoring of $IFS
> 
> Oh, God! Now I get what that strange “local X=$X” stands for! I thought it's
> just a mistake. Not enough practice in bash programming :(.
> 

It didn't help that I didn't emphasize that more.  Take it out and you'll see how the script reverts to the incorrect logic.

Anyhow, the more I think about this the more I realize that I like your way better because style does count for something when it brings clarity.

@WilliamH, let's leave this open a bit longer while I think about the cleanest way to do this.

BTW, to be clear, my script uses bash-isms which we should avoid in the init scripts.  Still, it does demonstrate the point.
Comment 9 Kirill Elagin 2011-06-27 21:28:12 UTC
IMO, you should consider three points:
1) Incorrect logic in localmount which effectively unset $IFS in almost all real-life cases.
2) 6 lines of code in mount-ro which _do absolutely nothing_.
3) Style and readability.
Comment 10 Anthony Basile gentoo-dev 2011-07-06 02:22:40 UTC
@openrc

Has everyone had a chance to look at this?  Looks okay to commit.  I'll go ahead and do so in a few days if there are no objections.
Comment 11 SpanKY gentoo-dev 2011-07-06 02:25:45 UTC
some broken shells will barf if IFS contains whitespace (like dash) when using `local` ...
Comment 12 Anthony Basile gentoo-dev 2011-07-07 10:48:43 UTC
(In reply to comment #11)
> some broken shells will barf if IFS contains whitespace (like dash) when using
> `local` ...

Apply the patch removing the local then?
Comment 13 William Hubbs gentoo-dev 2011-07-07 13:32:55 UTC
(In reply to comment #12)
> (In reply to comment #11)
> > some broken shells will barf if IFS contains whitespace (like dash) when using
> > `local` ...
> 
> Apply the patch removing the local then?

I don't think we want to get rid of "local". The patch in comment #7 seems to be more straight forward to me than what we have now; I am thinking that is what we want to apply?
Comment 14 Kirill Elagin 2011-07-07 13:34:49 UTC
(In reply to comment #11)
> some broken shells will barf if IFS contains whitespace (like dash) when using
> `local` ...

What are those shells?
Comment 15 William Hubbs gentoo-dev 2011-07-07 14:11:24 UTC
All,

I just noticed that if we apply the patch from comment #7, the only
reference to IFS in both scripts is the line that says:

local IFS=$IFS:

So that makes me wonder if we could take that patch further and just
remove that line from both scripts? It isn't clear to me that IFS is
still being used anywhere.
Comment 16 William Hubbs gentoo-dev 2011-07-07 14:27:57 UTC
You can disregard my previous comment; I see what IFS is now thanks to
Anthony. :-)
Comment 17 SpanKY gentoo-dev 2011-07-07 15:58:15 UTC
Comment on attachment 278391 [details, diff]
Fix saving and restoring of $IFS

(In reply to comment #14)

you know in the comment you quoted, i gave you a specific example right ?

the code you'll need to use is:
    local IFS="$IFS:"
Comment 18 William Hubbs gentoo-dev 2011-07-07 16:55:27 UTC
Created attachment 279343 [details]
0001-save-and-restore-IFS-correctly.patch

Mike and all,

this is an updated version of the patch.

Is this correct?

Thanks,

William
Comment 19 SpanKY gentoo-dev 2011-07-07 17:21:22 UTC
it's missing X tags referring to the bug report, but otherwise looks like OK based on what Kirill has said
Comment 20 William Hubbs gentoo-dev 2011-07-07 17:36:25 UTC
Ok, cool.

Anthony, are you ok with the last patch I attached? If so I'll go ahead
and apply it.
Comment 21 Anthony Basile gentoo-dev 2011-07-07 18:53:53 UTC
(In reply to comment #20)
> Ok, cool.
> 
> Anthony, are you ok with the last patch I attached? If so I'll go ahead
> and apply it.

Yep.
Comment 22 William Hubbs gentoo-dev 2011-07-07 19:56:34 UTC
This has been applied in git, commit 0c70328.