Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

CreateMode.REPLACE not respected on malformed input

Open
#159 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
android, kotlin
Domain
mobile

Research direction

Start at MediaStoreCompat.createDownload and reproduce the issue with a FileDescription name containing "?" and CreateMode.REPLACE, then inspect the returned storage file name. Determine the intended behavior when the OS renames malformed input; done means repeated calls no longer create "hello_(1)" unexpectedly, with the chosen behavior covered or documented.

Written by the indexing model from the issue text.

Description

CreateMode.REPLACE not respected on malformed input

When testing MediaStoreCompat.createDownload I found a bug caused by the OS renaming the name when it has invalid input.

Assume you have some regular function to create a file.

fun testDuplicate(context : Context) {
    val file = MediaStoreCompat.createDownload(
        context = context,
        file = FileDescription(
            name = "hello",
            "test",
            mimeType = "text/plain"
        ),
        mode = CreateMode.REPLACE
    )?.toStorageFile(context)

    file?.openOutputStream()?.writer()?.use { writer ->
        writer.write("Hello World!")
        writer.flush()
    }
}

This just creates a file named "hello.txt" with the content "Hello World!". And if called twice it replaces the file.

testDuplicate(context)
testDuplicate(context)

-> only 1 file

This is expected behavior, and everything is fine.

However if I now choose the name = "hello?" it creates the file fine, but the OS renames it to hello_, therefore the subsequent call to testDuplicate does not replace "hello_" because it is searching for "hello?" and therefore creates a new file called "hello_(1)".

The expected behavior is to either fail or replace the file named "hello_". However the current implementation creates another file even with the mode CreateMode.REPLACE.

This issue is fixed if the createDownload function also sanitized the file name like the os with e.g. .replace("?","_"), however I get that this is a wonky solution. I think it is sane to either document this as an issue, sanitize the filename, or even detect the creation of the wrong file name.

Dominant language
Kotlin
Stars
867
Forks
103
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from anggrayudi/SimpleStorage

All issues in anggrayudi/SimpleStorage

Similar issues

More Kotlin issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.