I was wondering about the copyright of code snippets from answers. Let's say you ask a question and the solution consists of ten lines of code, which are given in an answer. Are there any restrictions about the use of that code?

It seems that the content of this page is under the creative commons license. So does one have to attribute the author if code is used in an open source project? Can it be legally used in closed source projects at all?

I guess ten lines of code are not really problematic, but is there a critical number of lines?

Return to FAQ index


Anything that you post to Stack Overflow will be under the terms of the Creative Commons license. You can find more information by looking at the bottom right of the page and clicking the cc-wiki link and by looking at the bottom left of the page and clicking the legal link.

As far as more specific legal issues, you should probably consult an attorney.

  • 54
    So basically if I paste a 10-line snippet of code from stackoverflow into my project, I must now distribute my project onder CC by-sa as well?
    – davr
    Sep 24 '08 at 17:54
  • 1
    If you include a code snippet from another source, you can't arbitrarily for that content to Creative Commons ... it stays under it's original license until the copyright holder says otherwise.
    – Steve Moyer
    Sep 24 '08 at 17:55
  • 28
    Nothing here changes who owned the code before it was posted. If the poster didn't have the right to post it (because of his company's IP provisions, or because it was taken from elsewhere) the CC license doesn't stop the original owner from claiming infringement against someone who downloads.
    – Will M
    Sep 24 '08 at 18:03
  • 2
    @Davr - If you plan on releasing the code under a license that would be incompatible with the creative commons license, I wouldn't post it to StackOverflow. I would imagine that this wouldn't be a problem for most FOSS licenses. And if this is proprietary code, I wouldn't post it to begin with. :) Sep 24 '08 at 18:24
  • 63
    No...I'm talking about the other way around. I ask "How do I do Foo?", then someone answers with some source code, presumably licensed under CC by-sa. What happens when I take that source code, and add it to my non-opensource application?
    – davr
    Sep 24 '08 at 20:28
  • 5
    I modified a code I found on stackoverflow and add it to my program (which I intend to release under GPL). I mentioned in comments the person who wrote it and added the url to the topic on stackoverflow where I found it. Do I need to do anything else?
    – Adinan
    Nov 10 '13 at 15:43
  • 1
    @Jason: Realizing this wasn't Davr's question, I think your earlier comment missed something. IANAL, but afaict you can post a portion of closed source to SO and "dual license" (since it's all yours). If I have closed foo, sublicense foo to you, your posting foo to SO doesn't stop me from continuing to treat foo-superset as otherwise closed. Same if that "sublicensee" is myself. You can use foo on SO under CC, and I can use foo-superset as closed without suing myself. ;^) See MySql and dual licensing.
    – ruffin
    Nov 30 '13 at 17:12
  • 6
    @JasonBaker I would imagine that this wouldn't be a problem for most FOSS licenses. WRONG! From Creative Commons > Compatible Licenses, under "BY-SA" section "Version 3.0": Currently, no non-CC licenses have been designated as compatible with BY-SA 3.0. So you are effectively forbidden to use code posted by other/yourself on Stack Exchange and any sub-site in any projects unless it is licensed under and only under CC BY-SA 3.0 (because no other license is compatible).
    – zypA13510
    Mar 31 '18 at 20:50
  • 3
    Sorry, this does not really answer the questions and is more of an link-answer.
    – keuleJ
    Sep 12 '18 at 11:38

This is a software licensing nightmare. If you paste in snippets in your answer that are in an incompatible license to cc-wiki, you are violating the terms of SO. I would imagine there are probably thousands of examples of such violations.

In addition to that major issue, I personally like to choose how to license my work. I am happy for all the text to be the viral cc-wiki. But am very unhappy to have my little code samples or classes under cc-wiki.

To combat this issue I just added this snippet to my profile on SO

All original source snippets I post on stackoverflow are dedicated to the public domain. Do with them as you see fit.

All those who are against the viral nature of the source samples should explicitly say what license their samples are in.

Another option is to post any larger snippets to sites like gist and have a license in the header of the file.

  • 23
    The "Public Domain" is not a license. Licenses are a type of contracts. The Public Domain is the set of works that fall outside of copyright, for whatever reason (eg too old, not creative, explicitly put there). Even so, your intent is clear enough to put your code in it.
    – MSalters
    May 22 '09 at 8:10
  • 42
    "Public domain" does not exist in many countries, so if you want everybody to be able to use your code snippets without worries, consider using CC0 (if you like Creative Commons licences), WTFPL, Unlicence, or the like. Nov 30 '11 at 21:08
  • 2
    This is much more helpful than the accepted answer, but for such a re-dedication to be meaningful, it really needs to be a license in itself (as @ChrisJester-Young noted). The license closest to the public domain is the Unlicense.
    – bukzor
    Mar 10 '14 at 18:25
  • Thanks! I've added this statement to my profile as well, including the Unlicense specifically.
    – Chris
    May 5 '14 at 20:37
  • 3
    @ChrisJester-Young, Is WTFPL a real copyright that actually works in court?
    – Pacerier
    Jun 17 '14 at 16:16
  • 1
    @Pacerier (I'm not a lawyer.) The only free software licence that I'm aware of having been tested in court is GPL. The only time WTFPL will get tested in court is if a copyright holder reneges on the licence and tries to sue "after the fact", since technically there is no way for the licensee to violate the licence. I have not heard of such a case. Jun 17 '14 at 17:03
  • 2
    @ChrisJester-Young, So shouldn't we instead opt for GPL rather than Unlicense and friends?
    – Pacerier
    Jun 17 '14 at 19:03
  • 8
    @Pacerier No, because here, our intent (as copyright holders) is to enable everyone to do anything they want with our code snippets. Using the GPL won't achieve that. CC0, Unlicence, and WTFPL are supposed to be "as close to public domain as possible" licences. The GPL is not such a licence. Jun 17 '14 at 19:05
  • @ChrisJester-Young, Actually, effectively wouldn't WTFPL and Unlicence actually allow other people to start claiming that they created our work?
    – Pacerier
    Jun 18 '14 at 18:55
  • 3
    @Pacerier nope. In many countries, attribution, integrity and a few other such rights can not be changed or given away or such.
    – chx
    Oct 12 '14 at 0:26
  • @chx, However no one knows who is speaking the truth. They can claim that it is theirs and there's no way to counter-prove that it belongs to me.
    – Pacerier
    Oct 12 '14 at 5:45
  • @waffles, I don't think your technique works because you are publishing on SO. For it to work you must first publish it at somewhere else under "public domain", then copy it into your SO post. Otherwise it wouldn't hold.
    – Pacerier
    Apr 12 '15 at 12:18
  • 4
    That snippet only has value until someone comes along and edits your question, then the license goes back to cc-wiki again. Jun 8 '16 at 16:14
  • 2
    I would also make sure that you phrase that this is an addition to the Creative Commons "default" of stackoverflow. If you present your signature as voiding instead of expanding upon the stackoverflow terms of use, you may not have a legal footing.
    – Tormod
    Jan 15 '18 at 10:19
  • 1
    You might want to write that on all Stack Exchange profiles you have then: meta.stackexchange.com/questions/333089/… - all Stack Exchange websites use the same license. Very cool idea and I'm putting it on all my profiles now (with the WTFPL license though).
    – DADi590
    Dec 23 '20 at 21:32

If you are talking about taking code from Stack Overflow for use in your work, I would be very careful. Even if it is the opinion of the people here that posts to Stack Overflow are covered by Creative Commons, you will need to be able to prove that you got the code from here.

A Short Story To Illustrate:

  • Someone else decides the code is a good solution for a problem they are facing, and decides to copy it into their open source GnuFizzBuzz project, covered by the GPL.
  • Five years from now, someone is doing an open source audit on your code (maybe you are selling to a nervous customer, or your company is going public).
  • The open source audit finds the snippet of code you got from Stack Overflow, and recognizes it as originating in GnuFizzBuzz.
  • You are now stuck explaining how/why you have GPL code mixed into your commercial product, or proving that the code was actually from Stack Overflow. Can you prove that the person posting the code to Stack Overflow owned it to begin with (maybe they actually copied it from GnuFizzBuzz to begin wih).

It may sound unlikely, but I have seen this exact situation with my own eyes. The safe thing to do is to regard any code snippets you find as explanations of how something could work. Read, understand, and write a solution based on your understanding. Don't ever cut/paste code you found on the Internet if you are working on a commercial product.

  • 32
    ...so, if including code from SO, include the URL in a comment.
    – msh210
    Dec 19 '11 at 21:30
  • 2
    If you rely on specific code (and to which copyright applies), contact the author. CC-WIKI is not a software license and you want to have usage terms more clear than the CC-WIKI license is able to specify it.
    – hakre
    Jan 30 '12 at 15:05
  • 4
    In this answer (by Jeff Atwood actually) it's written that "a snippet of code falls under excerpt category and thus should be free to use under fair use" — but when is a snippet too long to be a snippet?
    – KajMagnus
    Feb 22 '13 at 11:00
  • 20
    @KajMagnus: That's a legal judgement, and it's wrong. Fair use doctrine does not apply to embedding excerpts of copyrighted work into source code.
    – bukzor
    Mar 10 '14 at 18:44
  • Would be interesting to either be able to choose the license to have content under, or to be able to have a signature option which would say something like, in my case, "All things originally from me posted on any Stack Exchange website are under the following license: [license name/link/whatever], not the default one.". I can put it on my profile, but will be lucky for anyone to go there check about it. Unless I manually write it, which I think it's weird, but maybe it's better than having it under the wrong license?
    – DADi590
    Dec 24 '20 at 20:19

This has been discussed on UserVoice already. Jeff closed the request with this response:

I would hope that people are not posting giant blocks of code at SO, making it more of a quote / fair use type situation.

For commercial work, you should see your company lawyer before using any code from SO.

  • 1
    Thanks for the uservoice reference, seems that this is as clear as it gets.
    – nikow
    Sep 30 '08 at 15:36
  • 11
    Its clear, but as the owner of a copyright you have the right to license your work under whichever license you see fit. (in addition to the cc-wiki on this site) I attached a disclaimer to my profile saying that I am giving away my snippets. I hope others follow suite
    – waffles
    May 22 '09 at 7:54
  • 1
    The only trouble is that there are legislations which unfortunately do not have a concept of "fair use". But I also agree that such small snippets will probably cause no problem, since all implementations of "standard" problems tend to look alike. I also want to hint to this CC FAQ about the use of CC licenses for software.
    – quazgar
    May 23 '13 at 11:26
  • 9
    Considering the Code Review site, where long/giant blocks of code is the norm, I'd say that Jeff's answer is quite outdated. Dec 19 '13 at 1:45
  • 2
    @SimonAndréForsberg, Exactly, we even have Stack Snippets now which indirectly encourages longer code.
    – Pacerier
    Apr 12 '15 at 12:16

Only a lawyer can answer this question. But, very relevant are fair use and whether a small snippet of code is copyrightable.

Also, the implicit license it was posted under is relevant when asking a lawyer their opinion. A lawyer might say that it would be difficult for someone to reasonably argue that their copyright was violated when code they post in response to a question is used in any way. Legal concepts such as promissory estoppel might come into play. It would also be interesting to see if reputation points could be deemed "consideration" under the law.

Patent issues are a much bigger risk, because the patent law is so nonsensical and keeps getting worse. Software patents are very dangerous and have already caused great damage to the software industry and free software.

Also, export restrictions (e.g. ITAR) are very dangerous, although some important precedents protecting source code as speech have been set. There was a time when simply having hooks to process data before sending it on its way was restricted as a munition in the US.

  • 5
    There is no implicit license. There's an explicit one - CC-BY-SA. Oct 15 '09 at 21:37

I am not a lawyer, but I think if you do a direct cut and paste of the code then you are going to have to look at what the site's cc-wiki gives you permission to do. Personally, I think that if you read the code, understand the code, and then use that knowledge to write you own take on what the code is doing you might skirt by the copyright issue as you aren't doing a cut and paste of the code.

However, it is worth noting that you do have to be aware that there might be a patent on the code in which case you have to be aware of those issues. That is an area were you are starting to look at having a lawyer render an option on it - if you work at a company then someone in the legal department might be able to give you an answer on the quesiton.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .