Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check for \relax in \pgfkeysifdefined #1132

Merged
merged 1 commit into from
Feb 1, 2022
Merged

Conversation

hmenke
Copy link
Member

@hmenke hmenke commented Jan 30, 2022

Motivation for this change

Fixes #1131

There are a lot of instances in pgfkeys where \csname is used, e.g. in \pgfkeysgetvalueof. This will \let the mentioned csname become \relax so we have to be prepared for this in \pgfkeysifdefined. Indeed, this was the original behavior before eTeX but it somehow got lost when transitioning to \ifcsname. The behavior is broken with eTeX since 641a883.

TODO:

  • Write a unit test

Checklist

Please signoff your commits to explicitly state your agreement to the Developer Certificate of Origin. If that is not possible you may check the boxes below instead:

@hmenke hmenke requested a review from muzimuzhi January 30, 2022 09:19
@hmenke hmenke added the pgfkeys label Jan 30, 2022
@hmenke hmenke requested a review from Skillmon January 30, 2022 11:43
@hmenke hmenke force-pushed the ifcsname branch 3 times, most recently from e10fbbe to 136fe0d Compare January 30, 2022 13:11
testfiles/gh-issue-1131.lvt Outdated Show resolved Hide resolved
testfiles/gh-issue-1131.lvt Outdated Show resolved Hide resolved
testfiles/gh-issue-1131.tlg Outdated Show resolved Hide resolved
tex/generic/pgf/utilities/pgfkeys.code.tex Show resolved Hide resolved
Copy link
Member

@muzimuzhi muzimuzhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth documenting the changed behavior of \pgfkeysgetvalue and \pgfkeysvalueof?

Sorry I didn't put all my thoughts in a single review.

tex/generic/pgf/utilities/pgfkeys.code.tex Outdated Show resolved Hide resolved
tex/generic/pgf/utilities/pgfkeys.code.tex Outdated Show resolved Hide resolved
testfiles/gh-issue-1131.lvt Show resolved Hide resolved
@hmenke hmenke force-pushed the ifcsname branch 2 times, most recently from 49695b7 to 9ddda00 Compare January 30, 2022 19:28
@muzimuzhi
Copy link
Member

@hmenke my bad, please revert my last force push

Comment on lines +193 to +194
\let\pgfkeys@relax\relax
\long\def\pgfkeysvalueof#1{\csname\pgfkeys@ifcsname{pgfk@#1}{pgfk@#1}{pgfkeys@relax}\endcsname}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added an alias for \relax for the unlikely event that someone actually does assign to this.

@hmenke hmenke requested review from muzimuzhi and Skillmon January 31, 2022 12:59
given csname become `\relax` in case it wasn't defined. This resulted in some
leakage of accidentally `\relax`ed keys into the global scope. This has been
cleaned up a little. To preserve compatibility macros that used to expand to a
`\relax`ed csname now expand to a primitive `\relax`. This affects the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outdated. Was changed to expand to \pgfkeys@relax (same four lines below, the last parts of this entry remain true however, this is a breaking change and one shouldn't make assignments to the results without testing for \relax or \pgfkeys@relax).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also: Should this get documented for \pgfkeysvalueof or should the documentation remain unchanged?

\ENDTEST

\BEGINTEST{pgfkeys: keys must be able to hold \relax}
\pgfkeyslet{/bar}{\relax} % or \pgfkeys{/bar/.initial=\relax}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the comment: \pgfkeys{/bar/.initial=\relax} doesn't \let to \relax but does \def\<key-path>{\relax}, so <key-path> doesn't have the \meaning \relax. More specifically: I'd expect \pgfkeysifdefined being false for \pgfkeyslet{<path>}{\relax} but true for \pgfkeys{<path>/.initial=\relax}. Maybe add a test for this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think letting a key to \relax should “undefine” it. For that it's better to let it to something that is actually undefined.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hmenke agreed that \pgfkeyslet{/bar}{\myundefinedmacro} is better, but \let\foo\relax will have the same result for the majority of tests (and for the tests that have been implemented by now in pgfkeys).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\pgfkeysifdefined has been using \ifcsname since 2007. I've not actually changed \pgfkeysifdefined but just cleaned up the accidental \relaxing. Now that I have squashed all the commits this should also be clearer.

There are a lot of instances in pgfkeys where \csname is used, e.g. in
\pgfkeysgetvalueof. This will \let the mentioned csname become \relax so we have
to be prepared for this in across the board in pgfkeys. Indeed, this was the
original behavior before eTeX but it somehow got lost when transitioning to
\ifcsname. The behavior is broken with eTeX since 641a883.

BREAKING CHANGE: \pgfkeysvalueof will expand to an alias for the primitve \relax
in case the key was undefined rather than to a \relaxed csname.

Co-authored-by: Jonathan Spratte <jspratte@yahoo.de>
Co-authored-by: muzimuzhi <muzimuzhi@gmail.com>
Signed-off-by: Henri Menke <henri@henrimenke.de>
@hmenke hmenke merged commit 1dbf7ae into pgf-tikz:master Feb 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

pgfkeys: Undefined .@cmd handler check is not robust
3 participants