[apparmor] [patch] prevent encoding errors when reading a file

Kshitij Gupta kgupta8592 at gmail.com
Fri Oct 10 18:28:21 UTC 2014


Hello,

On Fri, Oct 10, 2014 at 11:34 PM, Christian Boltz <apparmor at cboltz.de> wrote:
> Hello,
>
> Am Freitag, 10. Oktober 2014 schrieb Kshitij Gupta:
>> I think we can move this redundant version check part to module level
>> instead of function level.
>
> There is more redundant code - the only differences are the mode ('r'
> vs. 'w') and that ..._write() doesn't allow to specify the charset as
> parameter.
>
> This patch changes open_file_read() and open_file_write() to wrapper
> functions, and moves the "real" code to the new open_file_anymode()
> function.
>
> Also, I removed the try/except - it's superfluous because it throws the
> exception without any modifications.
>

Nice catch.

>
> Updated patch:
>
> === modified file 'utils/apparmor/common.py'
> --- utils/apparmor/common.py    2014-02-28 22:31:16 +0000
> +++ utils/apparmor/common.py    2014-10-10 17:59:47 +0000
> @@ -168,19 +168,21 @@
>
>  def open_file_read(path, encoding='UTF-8'):
>      '''Open specified file read-only'''
> -    try:
> -        orig = codecs.open(path, 'r', encoding)
> -    except Exception:
> -        raise
> -
> -    return orig
> +    return open_file_anymode('r', path, encoding)
>
>  def open_file_write(path):
>      '''Open specified file in write/overwrite mode'''
> -    try:
> -        orig = codecs.open(path, 'w', 'UTF-8')
> -    except Exception:
> -        raise
> +    return open_file_anymode('w', path, 'UTF-8')
> +
> +def open_file_anymode(mode, path, encoding='UTF-8'):
> +    '''Open specified file in specified mode'''
> +
> +    errorhandling = 'surrogateescape'
> +    if sys.version_info[0] < 3:
> +        errorhandling = 'replace'
> +
> +    orig = codecs.open(path, mode, encoding, errors=errorhandling)
> +
>      return orig
>
>  def readkey():
>
>
Thanks for the patch (and the quick follow-up patch) and cleanup.

Acked-by: Kshitij Gupta <kgupta8592 at gmail.com>.

Regards,
Kshitij Gupta
>
> Regards,
>
> Christian Boltz
> --
> Wenn es jemand gibt, der Facebook derzeit noch stoppen kann, dann wohl
> Google. Regentraufen- und Pestcholera-Vergleich bitte hier einfügen.
> [http://praegnanz.de/weblog/ein-tag-vier-interessante-news]
>
>
> --
> AppArmor mailing list
> AppArmor at lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/apparmor



More information about the AppArmor mailing list