Skip to content

Handle failed iconv conversion. Unhandled execution path was UB, causing a segfault for me#5329

Merged
skef merged 5 commits intofontforge:masterfrom
ViliusSutkus89:master
Jan 10, 2024
Merged

Handle failed iconv conversion. Unhandled execution path was UB, causing a segfault for me#5329
skef merged 5 commits intofontforge:masterfrom
ViliusSutkus89:master

Conversation

@ViliusSutkus89
Copy link
Copy Markdown
Contributor

Type of change

  • Bug fix

Hello,

FontForge does some locale conversions before printing errors. NOUI_IError and NOUI__LogError (in nouiutil.c) calls utf82def_copy (in ucharmap.c), which calls do_iconv.

do_iconv handles failed iconv call and returns NULL, which propagates back to NOUI_IError and NOUI__LogError functions and is not handled there.

NOUI_IError and NOUI__LogError are calling printf with NULL as an argument to "%s", which is undefined behaviour and that UB is crashing my Android devices.

Yes, I do understand, that I should fix the iconv call to not fail, but until then, I wouldn't mind fixing this UB, if that looks acceptable.

Regards,
Vilius

…ing a segfault for me

FontForge does some locale conversions before printing errors.
NOUI_IError and NOUI__LogError (in nouiutil.c) calls utf82def_copy (in ucharmap.c), which calls do_iconv.
do_iconv handles failed iconv call and returns NULL, which propagates back to NOUI_IError and NOUI__LogError functions and is not handled there.
NOUI_IError and NOUI__LogError are calling printf with NULL as an argument to "%s", which is undefined behaviour and that UB is crashing my Android devices.

Yes, I do understand, that I should fix the iconv call to not fail, but until then, I wouldn't mind fixing this UB, if that looks acceptable.
@skef
Copy link
Copy Markdown
Contributor

skef commented Jan 4, 2024

This looks good to me, but could you rebase @ViliusSutkus89 ? I don't want to override the checks on all of the back-log.

Comment thread fontforge/nouiutil.c Outdated
putc('\n',stderr);
free(str);
} else {
fprintf(stderr, "utf82def_copy failure!\n");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Throwing the error out because iconv() has opinions doesn't seem right to me. Maybe add fprintf(stderr,"%s",buffer); here, just to print whatever we have, even if badly encoded?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Only issue I could see would be malformed characters being interpreted as terminal control characters

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@ViliusSutkus89, do you have a sample font + steps to reproduce this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have a broken iconv that's causing the issue :D But it is still UB.
The original error message is "The PostScript font name "BGDGBW弫Roboto-Bold" is invalid.\nIt should be printable ASCII,\nmust not contain (){}[]<>%/ or space\nand must be shorter than 63 characters"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Broken iconv would mean that any error message would trigger UB here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I understand that we use iconv() mainly for two things: file names and error messages to the terminal. Maybe we can add separate iconv_t descriptors for errors using ASCII//TRANSLIT as a fallback? In that case your error message would become "The PostScript font name "BGDGBW?Roboto-Bold...", which is still informative.

@iorsh
Copy link
Copy Markdown
Contributor

iorsh commented Jan 4, 2024

Related issue: #5326

@iorsh
Copy link
Copy Markdown
Contributor

iorsh commented Jan 5, 2024

This patch fixes #5326 (which calls utf82def_copy() elsewhere) and should fix this segfault too:
utf82def_copy_safe.patch.gz

@ViliusSutkus89, could you please apply and verify with your font?

@ViliusSutkus89
Copy link
Copy Markdown
Contributor Author

@iorsh , yes! this works for me. I still get "utf82def_copy failure!", but that's because of my broken iconv

@iorsh
Copy link
Copy Markdown
Contributor

iorsh commented Jan 9, 2024

@iorsh , yes! this works for me. I still get "utf82def_copy failure!", but that's because of my broken iconv

@ViliusSutkus89 Great, could you please commit the patch into your PR? I don't have access.

@ViliusSutkus89
Copy link
Copy Markdown
Contributor Author

Sure thing, @iorsh . I've also pulled commits from current master branch, for a cleaner merge.

Copy link
Copy Markdown
Contributor

@skef skef left a comment

Choose a reason for hiding this comment

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

lgtm

@skef skef merged commit c456546 into fontforge:master Jan 10, 2024
@ViliusSutkus89
Copy link
Copy Markdown
Contributor Author

Sweet, thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants